本文原創, 轉載請註明出處:http://blog.csdn.net/qinjuning
譯二:
使用<include />標籤複用布局檔案
翻譯地址:http://developer.android.com/training/improving-layouts/reusing-layouts.html#Merge
儘管Android通過內建了各種各樣的控制項提供了微小、可複用的互動性元素,也許你需要複用較大的
組件 ---- 某些特定布局檔案 。為了更有效率複用的布局檔案,你可以使用<include />以及<merge /> 標籤將其他的布局檔案加入到當前的布局檔案中。
複用布局檔案是一種特彆強大的方法,它允許你建立可複用性的布局檔案。例如,一個包含“Yse”or“No”的 Button面版,或者是帶有文字說明的 Progressbar。複用布局檔案同樣意味著你應用程式裡的任何元素都能從 繁雜的布局檔案提取出來進行單獨管理,接著你需要做的只是加入這些獨立的布局檔案(因為他們都是可複用地)。 因此,當你通過自訂View建立獨立的UI組件時,你可以複用布局檔案讓事情變得更簡單。
1、建立一個可複用性的布局檔案
如果你已經知道複用布局的”面貌”,那麼建立、定義布局檔案( 命名以”.xml”為尾碼)。例如,這裡是一個來自 G- Kenya
codelab 的布局檔案,定義了在每個Activity中都要使用的一個自訂標題 (titlebar.xml):由於這些 可複用性布局被添加至其他布局檔案中,因此,它的每個根視圖(root View)最好是精確(exactly)的。
<FrameLayout xmlns:android="http://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>
2、使用<include />標籤
在需要添加這些布局的地方,使用<include />標籤 。 例如,下面是一個來自G-Kenya codelab的布局檔案,
它複用了上面列出的“title bar”檔案, 該布局檔案如下:
<LinearLayout xmlns:android="http://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>
你也可以在<include />節點中為被添加的布局檔案的root View定義特別標識,重寫所有layout參數即可(任何 以“android:layout_”為首碼的屬性)。例如:
<include android:id=”@+id/news_title” android:layout_width=”match_parent” android:layout_height=”match_parent” layout=”@layout/title”/>
3、使用<merge />標籤
當在布局檔案中複用另外的布局時, <merge />標籤能夠在布局層次消除多餘的視圖元素。例如,如果你的
主布局檔案是一個垂直地包含兩個View的LinearLayout,該布局能夠複用在其他布局中,而對任意包含兩個View的 布局檔案都需要一個root View(否則, 編譯器會提示錯誤)。然而,在該可複用性布局中添加一個LinearLayout 作為root View,將會導致一個垂直的LinearLayout包含另外的垂直LinearLayout。內嵌地LinearLayout只能減緩 UI效率,其他毫無用處可言。 該複用性布局利用.xml呈現如下:
<LinearLayout xmlns:android="http://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 />標籤的布局檔案:
<merge xmlns:android="http://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 />節點。
另外的,按需載入View視圖 ,請看:
http://developer.android.com/training/improving-layouts/loading-ondemand.html
如何使ListView流暢滑動 ,請看:
http://developer.android.com/training/improving-layouts/smooth-scrolling.html