fragment不能单独存在,它的生命周期受到Activity影响。碎片可以进行重用。如果当前的activity处于停止状态,或者销毁状态,则Fragments也亦是如此。
第一种方式:使用fragment标签方式:
定义两个布局文件:
第一个Left
<?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:background="#566754"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="left" /
</LinearLayout>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#566754"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="left" /
</LinearLayout>
定义第二个布局文件:
<?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:background="#aa6567"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="right" />
</LinearLayout>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#aa6567"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="right" />
</LinearLayout>
分别对应的代码:
left->
public class left extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.left_, container, false); //加载布局
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
right->
public class right extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.right_, container, false); //加载布局
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
参数解释:
int resource:加载xml布局文件资源id
ViewGroup root:在Activity中Fragment的容器
Boolean attachToRoot:是否将加载的View对象放进root中
在主的布局文件中定义
<LinearLayout
android:id="@+id/line"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<fragment
android:id="@+id/F_left"
android:name="fr.left" --》对应包名下的左面布局java处理代码
android:layout_width="40dp"
android:layout_height="match_parent" />
<fragment
android:id="@+id/F_right"
android:name="
fr.right"
--》对应包名下右面布局java处理代码
android:layout_width="50dp"
android:layout_height="match_parent" />
</LinearLayout>
第二种方式:使用FragmentMaanger,使用上下文对象获得
定义事物对象manger=getFragmentManager()
FragmentTransaction transation=manger.beginTransaction();
把布局添加
transation.add(R.id.CCCC,new FirstFragment())
替换:先移除后添加
transation.replace(R.id.CCC,new XXXXXFragment);
transation.remove( XXXFragment);
提交事务
transation.commit();
第三种方式,使用Support.V4包
首先
继承FragmentAcitvity类,
获取FragMentManger对象-》getSupportFramentManager();
开启事务
FragmentTransaction fragmentTransaction =frammentManger.beginTransacion()
替换,删除,添加,提交和上面相同。