在小屏幕設備中,activity通常填充整個屏幕。而這個activity是一個視圖的容器。
為了更好地在平板電腦上組織UI,我們可以使用“mini-activities”。每個mini-activities包含自己的一組視圖。
一個activity可以包含一個或多個這些mini-activities。這些mini-activities被稱為碎片(Fragment)。
碎片可以包含視圖,就像activity一樣。碎片總是會嵌入在一個activity中。
碎片形成用戶界面的原子單位,可以在activity中動態(tài)添加或刪除。
我們可以將Android中的片段的概念視為桌面用戶界面中的面板。
以下代碼顯示了碎片的基本用法。
在 res/layout
文件夾中,添加一個新文件并將其命名為 fragment1.xml
。填寫以下內(nèi)容:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#00FF00" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is fragment #1" android:textColor="#000000" android:textSize="25sp" /> </LinearLayout>
另外在 res/layout
文件夾中,添加另一個新文件,并將其命名為 fragment2.xml
。填充如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFFE00" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is fragment #2" android:textColor="#000000" android:textSize="25sp" /> </LinearLayout>
在 activity_main.xml
中,添加以下代碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <fragment android:name="cn.w3cschool.myapplication3.app.Fragment1" android:id="@+id/fragment1" android:layout_weight="1" android:layout_width="0px" android:layout_height="match_parent" /> <fragment android:name="cn.w3cschool.myapplication3.app.Fragment2" android:id="@+id/fragment2" android:layout_weight="1" android:layout_width="0px" android:layout_height="match_parent" /> </LinearLayout>
在 com.java2s.Fragments
包名稱下,添加兩個Java類文件并將其命名為 Fragment1.java
和 Fragment2.java
,將以下代碼添加到 Fragment1.java
:
package cn.w3cschool.myapplication3.app; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //Inflate the layout for this fragment return inflater.inflate(R.layout.fragment1, container, false); } }
將以下代碼添加到 Fragment2.java
:
package cn.w3cschool.myapplication3.app; // www.o2fo.com import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //Inflate the layout for this fragment return inflater.inflate( R.layout.fragment2, container, false); } }
一個碎片的行為非常像一個activity,它有:
XML文件包含你預期從某個activity獲得的所有常用UI元素:TextView,EditText,Button等。
碎片的Java類需要擴展 Fragment
基類:
public class Fragment1 extends Fragment { }
除了 Fragment
基類,碎片還可以擴展一些Fragment
類的其他幾個子類,例如 DialogFragment
,ListFragment
和PreferenceFragment
。
要繪制碎片的UI,請覆蓋 onCreateView()
方法。此方法需要返回一個View對象。
你可以使用LayoutInflater對象從指定的XML文件中擴充UI。要向activity添加片段,請使<fragment>
元素。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > /* www.o2fo.com*/ <fragment android:name="cn.w3cschool.Fragments.Fragment1" android:id="@+id/fragment1" android:layout_weight="1" android:layout_width="0px" android:layout_height="match_parent" /> <fragment android:name="cn.w3cschool.Fragments.Fragment2" android:id="@+id/fragment2" android:layout_weight="1" android:layout_width="0px" android:layout_height="match_parent" /> </LinearLayout>
每個碎片需要唯一的標識符。
更多建議: