1:什么是Fragment,Fragment是一种可以嵌入在活动中的UI片段,能够让程序更加合理和充分地利用大屏幕的空间,出现的初衷是为了适应大屏幕的平板电脑,可以将其看成一个小型Activity,又称作Activity片段。
2:Fragment依赖于Activity,不能独立存在,一个Activity可以有多个Fragment,一个Fragment可以被多个Activity重用,Fragment有自己的生命周期,并能接收输入事件,可以在Activity运行时动态地添加或删除Fragment
3:Fragment的生命周期
生命周期我们下次再来细说。
3:接下来就写一个比较简单的实例
(1)activity_main.xml
<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"
android:orientation="vertical"
tools:context="com.example.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:gravity="center">
<TextView
android:id="@+id/text_id&#