1、布局文件 a) /res/layout/fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#00ff00"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment1"/>
</LinearLayout> b) /res/layout/fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@android:color/black"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment2"
android:textColor="#ff0000"
/>
</LinearLayout> c) /res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment android:name="cn.zengfansheng.fragment.Fragment1"
android:id="@+id/fragment1"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
/>
<fragment android:name="cn.zengfansheng.fragment.Fragment2"
android:id="@+id/fragment2"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
/>
</LinearLayout> |