使用include標籤重用layouts

編寫:allenlsy - 原文:http://developer.android.com/training/improving-layouts/reusing-layouts.html

雖然 Android 提供很多小的可重用的交互組件,你仍然可能需要重用複雜一點的組件,這也許會用到 Layout。為了高效重用整個的 Layout,你可以使用 <include/><merge/> 標籤把其他 Layout 嵌入當前 Layout。

重用 Layout 非常強大,它讓你可以創建複雜的可重用 Layout。比如,一個 yes/no 按鈕面板,或者帶有文字的自定義進度條。這也意味著,任何在多個 Layout 中重複出現的元素可以被提取出來,被單獨管理,再添加到 Layout 中。所以,雖然可以添加一個自定義 View 來實現單獨的 UI 組件,你可以更簡單的直接重用某個 Layout 文件。

創建可重用 Layout

如果你已經知道你需要重用的 Layout,就先創建一個新的 XML 文件並定義 Layout 。比如,以下是一個來自 G-Kenya codelab 的 Layout,定義了一個需要添加到每個 Activity 中的標題欄(titlebar.xml):

<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>

根節點 View 就是你想添加入的 Layout 類型。

使用<include>標籤

使用 <include> 標籤,可以在 Layout 中添加可重用的組件。比如,這裡有一個來自 G-Kenya codelab 的 Layout 需要包含上面的那個標題欄:

<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>

你也可以覆寫被添加的 Layout 的所有 Layout 參數(任何 android:layout_* 屬性),通過在 <include/> 中聲明他們來完成。比如:

<include android:id="@+id/news_title"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         layout="@layout/title"/>

然而,如果你要在 <include> 中覆寫某些屬性,你必須先覆寫 android:layout_heightandroid:layout_width

使用<merge>標籤

<merge /> 標籤在你嵌套 Layout 時取消了 UI 層級中冗餘的 ViewGroup 。比如,如果你有一個 Layout 是一個豎直方向的 LinearLayout,其中包含兩個連續的 View 可以在別的 Layout 中重用,那麼你會做一個 LinearLayout 來包含這兩個 View ,以便重用。不過,當使用一個 LinearLayout 作為另一個 LinearLayout 的根節點時,這種嵌套 LinearLayout 的方式除了減慢你的 UI 性能外沒有任何意義。

為了避免這種情況,你可以用 <merge> 元素來替代可重用 Layout 的根節點。例如:

<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>

現在,當你要將這個 Layout 包含到另一個 Layout 中時(並且使用了 <include/> 標籤),系統會忽略 <merge> 標籤,直接把兩個 Button 放到 Layout 中 <include> 的所在位置。


书籍推荐