Fragment即碎片,片段的意思。如果有web开发经验的人,比较好理解,把Activity比如为一个html页面,fragment可以看成是一个div,一个一个的嵌在页面中。
本文摘录了一些网上博客的内容,如觉得侵权,请告知,必删除。
(一)使用xml布局文件来创建Fragment
1.先在Activity的布局xml中引入fragment
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/id_fragment_title"
android:name="com.example.fragementdemo.TitleFragement"
android:layout_width="fill_parent"
android:layout_height="45dp" />
<fragment
android:layout_below="@id/id_fragment_title"
android:id="@+id/id_fragment_content"
android:name="com.example.fragementdemo.ContentFragement"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
使用fragment标签,即可声明,name就是这个fragment对应的类
2.创建类,该类用于创建fragment
public class TitleFragement extends Fragment{
private ImageButton mLeftMenu;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_title, container, false);
mLeftMenu = (ImageButton) view.findViewById(R.id.left_btn);
mLeftMenu.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
Toast.makeText(getActivity(),
"i am an ImageButton in TitleFragment ! ",
Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
该类继承Fragment,这里暂时只实现onCreateView方法,类似Activity的onCreate方法
3.创建fragment的布局文件,不然在Fragment类中无法布局,这里以fragment_title为例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="使用Fragment做主面板"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
4.在Activity中只要正常因为Activty的布局文件即可。启动效果如图
(二)动态创建Fragment,在代码中创建
1.创建一个布局xml,里面有一个layout,该layout专门用于放fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/second_fragment">
</LinearLayout>
2.创建fragment的布局xml,表示这个fragment显示什么内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是第二个fragment,采用动态添加的方式"/>
</LinearLayout>
3.创建fragment的类,专门用于影射(创建)fragment
public class SecondFragment extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.second_fragment,container,false);
return view;
}
}
4.在Activity的类中处理,添加fragment到指定的地方
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
FragmentManager fm=getFragmentManager();
FragmentTransaction fs=fm.beginTransaction();
Fragment fragment=new SecondFragment();
fs.add(R.id.second_fragment,fragment);
fs.commit();
}
以上即可动态添加fragment到指定的地方