一、创建Fragment
//继承fragment类
public class Fragment01 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//返回布局view View view=View.inflate(getActivity(), R.layout.layout1, null); return view; } }
二、在activity中使用fragment
1、//创建fragmentd对象
Fragment01 fg=new Fragment01();
2、 //获取fragment管理器
FragmentManager fm=getFragmentManager();
3、开启事务
FragmentTransaction ft=fm.beginTransaction();
4、把fragment显示至界面
ft.replace(R.id.fragment1,fg);
5、提交
ft.commit();
//注意:如果使用android 4.3 一下版本,Fragment需要继承FragmentActivity,相关的类需要导入support包