使用<include />標簽復用佈局文件
盡管Android通過內置瞭各種各樣的控件提供瞭微小、可復用的交互性元素,也許你需要復用較大的
組件 —- 某些特定佈局文件 。為瞭更有效率復用的佈局文件,你可以使用<include />以及<merge />
標簽將其他的佈局文件加入到當前的佈局文件中。
復用佈局文件是一種特別強大的方法,它允許你創建可復用性的佈局文件。例如,一個包含“Yse”or“No”的
Button面版,或者是帶有文字說明的 Progressbar。復用佈局文件同樣意味著你應用程序裡的任何元素都能從
繁雜的佈局文件提取出來進行單獨管理,接著你需要做的隻是加入這些獨立的佈局文件(因為他們都是可復用地)。
因此,當你通過自定義View創建獨立的UI組件時,你可以復用佈局文件讓事情變得更簡單。
1、創建一個可復用性的佈局文件
如果你已經知道復用佈局的”面貌”,那麼創建、定義佈局文件( 命名以”.xml”為後綴)。例如,這裡是一個來自
G- Kenya codelab 的佈局文件,定義瞭在每個Activity中都要使用的一個自定義標題 (titlebar.xml):由於這些
可復用性佈局被添加至其他佈局文件中,因此,它的每個根視圖(root View)最好是精確(exactly)的。
2、使用<include />標簽
在需要添加這些佈局的地方,使用<include />標簽 。 例如,下面是一個來自G-Kenya codelab的佈局文件,
它復用瞭上面列出的“title bar”文件, 該佈局文件如下:
[java]
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:background="@color/titlebar_bg">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/gafricalogo" />
</FrameLayout>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:background="@color/titlebar_bg">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/gafricalogo" />
</FrameLayout>
你也可以在<include />節點中為被添加的佈局文件的root View定義特別標識,重寫所有layout參數即可(任何
以“android:layout_”為前綴的屬性)。例如:
[java]
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background="@color/app_bg"
android:gravity="center_horizontal">
<include layout="@layout/titlebar"/>
<TextView android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:text="@string/hello"
android:padding="10dp" />
…
</LinearLayout>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background="@color/app_bg"
android:gravity="center_horizontal">
<include layout="@layout/titlebar"/>
<TextView android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:text="@string/hello"
android:padding="10dp" />
…
</LinearLayout>
3、使用<merge />標簽
當在佈局文件中復用另外的佈局時, <merge />標簽能夠在佈局層次消除多餘的視圖元素。例如,如果你的
主佈局文件是一個垂直地包含兩個View的LinearLayout,該佈局能夠復用在其他佈局中,而對任意包含兩個View的
佈局文件都需要一個root View(否則, 編譯器會提示錯誤)。然而,在該可復用性佈局中添加一個LinearLayout
作為root View,將會導致一個垂直的LinearLayout包含另外的垂直LinearLayout。內嵌地LinearLayout隻能減緩
UI效率,其他毫無用處可言。
該復用性佈局利用.xml呈現如下:
[java]
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background="@color/app_bg"
android:gravity="horizontal">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/add"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/delete"/>
</LinearLayout>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background="@color/app_bg"
android:gravity="horizontal">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/add"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/delete"/>
</LinearLayout>
為瞭避免冗餘的佈局元素,你可以使用<merge />作為復用性佈局文件地root View 。例如:
使用<merge />標簽的佈局文件:
[java]
<merge xmlns:android="https://schemas.android.com/apk/res/android">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/add"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/delete"/>
</merge>
<merge xmlns:android="https://schemas.android.com/apk/res/android">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/add"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/delete"/>
</merge>
現在,當你添加該佈局文件時(使用<include />標簽),系統忽略< merge />節點並且直接添加兩個Button去
取代<include />節點。