每日日报
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>
本文作者:durtime
本文链接:https://www.cnblogs.com/durtime/p/14836470.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步