读了上一篇文章《Fragment基础详解》,相信大家对Fragment有了一定的了解,接下来让我们来看看Fragment的静态实现。
MainActivity.java
package hongbao.com.hongbao; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="hongbao.com.hongbao.MainActivity"> <fragment android:id="@+id/my_fragment1" android:name="hongbao.com.hongbao.MyFragment1" android:layout_width="match_parent" android:layout_height="100dp" /> <fragment android:id="@+id/my_fragment2" android:name="hongbao.com.hongbao.MyFragment2" android:layout_width="match_parent" android:layout_height="100dp" android:layout_below="@+id/my_fragment1"/> <fragment android:id="@+id/my_fragment3" android:name="hongbao.com.hongbao.MyFragment3" android:layout_width="match_parent" android:layout_height="300dp" android:layout_below="@+id/my_fragment2"/> </RelativeLayout>
MyFragment1.java
package hongbao.com.hongbao; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MyFragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.myfragment1_layout, container, false); } }
myfragment1_layout.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimaryDark"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="This is Fragment1" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
MyFragment2.java
package hongbao.com.hongbao; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MyFragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.myfragment2_layout, container, false); } }
myfragment2_layout.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorAccent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="This is Fragment2" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
MyFragment3.java
package hongbao.com.hongbao; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MyFragment3 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.myfragment3_layout, container, false); } }
myfragment3_layout.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimary"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="This is Fragment3" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
运行结果: