参考:
33.3-Fragment的创建-静态创建2_哔哩哔哩_bilibili
Fragment的创建_从现有代码创建foutran-CSDN博客
【Android】Fragment的基本用法、Fragment和活动间的通信、Fragment的生命周期、动态加载布局的技巧_android fragment-CSDN博客
文章目录
Fragment的静态创建
- 创建一个Fragment
- 布局代码中用一个容器承接并绑定
- fragment标签
- FragmentContainView标签
先创建一个导航界面
先创建一个带按钮的空白界面吧!

然后创建一个Fragment

创建一个Activity用来承载Fragment
由于Fragment依赖于Activity存在,故而我们首先需要创建一个Activity用来承载fragment。
Be like:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<fragment
android:id="@+id/my_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.fragment.fragment.StaticFragment1"/>
</androidx.constraintlayout.widget.ConstraintLayout>
做到这一步,还并没有感觉到Fragment跟普通的Activity有什么区别,我们给fragment加点控件再看看。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".fragment.StaticFragment1">
<LinearLayout

最低0.47元/天 解锁文章


被折叠的 条评论
为什么被折叠?



