码迷,mamicode.com
首页 > 移动开发 > 详细

android中的2种fragment部署方法

时间:2015-06-06 09:14:03      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

1.静态类部署方法:

layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.filedirtest.Fragment2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.20" />

</LinearLayout>

class:

import android.app.Fragment;    //注意这里导入的是此fragment

public static class Selffragment extends Fragment {
public Selffragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View root=inflater.inflate(R.layout.prefrence_fragment, container,false);
return root;
}
}

2.动态创建:

fragment类:

public class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView=inflater.inflate(R.layout.fragment_layout, container, false);
return rootView;

}
}

activity中创建;

ExampleFragment fragment = new ExampleFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragmentContainer, fragment).commit();

android中的2种fragment部署方法

标签:

原文地址:http://blog.csdn.net/ligaogang/article/details/46382843

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!