【1】MainActivity 布局 创建2个fragment 分配位置
fragment里面的name属性:要指定你声明的fragment,就是要加载这个fragment
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.xiaoshuai2.MainActivity">
<fragment
android:id="@+id/list"
android:name="com.xiaoshuai2.Demo1Fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<fragment
android:id="@+id/verwer"
android:name="com.xiaoshuai2.Demo2Fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
【2】创建相对应的Fragment ,Fragment不需要在AndroidManifest里面添加属性
//fragment代表Activity的一部分
public class Demo1Fragment extends Fragment {
//在这个方法里面初始化Fragment要展示的内容 相当于Activity的onCreate方法
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//[1]通过一个布局填充器 把一个布局转换成一个view对象
View view = inflater.inflate(R.layout.fragment_demo1, null);
return view;
}
}