fragment入门
通过xml文件声明fragment
创建Fragment的布局文件 xml
①创建一个类继承Fragment
②重写onCreateView方法 把fragment对应的xml布局文件加载进来 在这个方法中创建fragment的界面
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//把fragment 对应的xml布局文件文件 转换为View对象 加载到界面上
View view = inflater.inflate(R.layout.fragment_first, null);
return view;
}
}
③ 在activity的布局文件中 声明一个fragment节点 ( fragment要小写 一定要声明一个id )
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.itheima.fragmentdemo.FirstFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.itheima.fragmentdemo.SecondFragment"
android:id="@+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>