Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 佈局優化實戰:用 merge 標籤砍掉多餘的 View 層級

編輯:關於 Android 程式設計

稍前的文章已經談過如何借助 <include /> 標籤來重用與共享佈局程式碼。本篇接著說明 <merge /> 標籤的用法,並示範它與 <include /> 互補搭配時能發揮出的威力。

先認識 <merge /> 標籤

<merge /> 的職責相當單純:削減 View 樹的層級,讓 Android 佈局更精簡。看一個具體例子,你就能立刻明白它解決的是什麼問題。下面的 XML 要在圖片上疊一行標題,結構並不複雜——FrameLayout 裡放一個鋪滿父容器的 ImageView,其上再壓一個 TextView:

<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="center"
        android:src="@drawable/golden_gate" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dip"
        android:layout_gravity="center_horizontal|bottom"
        android:padding="12dip"
        android:background="#AA000000"
        android:textColor="#ffffffff"
        android:text="Golden Gate" />
</FrameLayout>

渲染出來的畫面很漂亮,乍看之下挑不出毛病:

HierarchyViewer 揭露的真相

但一旦拿 HierarchyViewer 工具來檢視,事情就有趣了。仔細端詳 View 樹會發現:我們在 XML 裡定義的那個 FrameLayout(圖中藍色高亮部分),居然是另一個 FrameLayout 的唯一子節點。

既然這個 FrameLayout 的尺寸和父容器完全相同(拜 fill_parent 常數所賜),又沒有設定任何 background、額外 padding 或 gravity,那它其實毫無用處,只是白白把 UI 結構搞得更複雜。問題來了:怎麼把它拿掉?畢竟 XML 文件必須有一個根標籤,而 XML 佈局也總是會對應到某個 View 實例。

merge 標籤登場

這正是 <merge /> 的用武之地。當 LayoutInflater 遇到這個標籤時,它會直接跳過它,把 <merge /> 內部的元素掛到 <merge /> 的父容器上。有點抽象?那就把 FrameLayout 換成 <merge />,改寫剛才的佈局:

<merge xmlns:android="https://schemas.android.com/apk/res/android">
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="center"
        android:src="@drawable/golden_gate" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dip"
        android:layout_gravity="center_horizontal|bottom"
        android:padding="12dip"
        android:background="#AA000000"
        android:textColor="#ffffffff"
        android:text="Golden Gate" />
</merge>

改寫之後,TextView 與 ImageView 會被直接加進上一層的 FrameLayout。畫面看起來一模一樣,但 View 層級明顯清爽了:

這裡之所以敢用 <merge />,關鍵在於 Activity 的 ContentView 其父容器永遠是 FrameLayout。反過來說,如果你的佈局是以 LinearLayout 之類的標籤作為根節點(舉例),這招就用不上了。除此之外,<merge /> 在別的場合也很好用,例如與 <include /> 搭配時幾乎天衣無縫;另外,在打造自訂的組合式 View 時,它也是一把利器。

實作一個 OkCancelBar 自訂 View

接下來看一個用 <merge /> 造出新 View 的例子——OkCancelBar,它包含兩個按鈕,並允許呼叫端自訂按鈕文字。下面的 XML 把這個自訂 View 疊在一張圖片上:

<merge xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:okCancelBar="https://schemas.android.com/apk/res/com.example.android.merge">
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="center"
        android:src="@drawable/golden_gate" />
    <com.example.android.merge.OkCancelBar
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:paddingTop="8dip"
        android:gravity="center_horizontal"
        android:background="#AA000000"
        okCancelBar:okLabel="Save"
        okCancelBar:cancelLabel="Don't save" />
</merge>

視覺效果如下:

OkCancelBar 的程式碼並不長,因為兩個按鈕是在外部 XML 中定義、透過 LayoutInflater 載入的。如以下片段所示,R.layout.okcancelbar 以 OkCancelBar 自身作為父容器:

public class OkCancelBar extends LinearLayout {

    public OkCancelBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        setWeightSum(1.0f);
        LayoutInflater.from(context).inflate(R.layout.okcancelbar, this, true);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.OkCancelBar, 0, 0);

        String text = array.getString(R.styleable.OkCancelBar_okLabel);
        if (text == null) text = "Ok";
        ((Button) findViewById(R.id.okcancelbar_ok)).setText(text);

        text = array.getString(R.styleable.OkCancelBar_cancelLabel);
        if (text == null) text = "Cancel";
        ((Button) findViewById(R.id.okcancelbar_cancel)).setText(text);

        array.recycle();
    }
}

兩個按鈕的定義如下。可以注意到,我們用 <merge /> 把兩顆按鈕直接塞進 OkCancelBar,而兩者都來自同一份外部佈局檔,維護時只要改一處即可,僅需覆寫各自的 id:

<merge xmlns:android="https://schemas.android.com/apk/res/android">
    <include layout="@layout/okcancelbar_button" android:id="@+id/okcancelbar_ok" />
    <include layout="@layout/okcancelbar_button" android:id="@+id/okcancelbar_cancel" />
</merge>

就這樣,我們做出一個既靈活又好維護、而且 View 層級相當精簡的自訂 View:

兩個必須牢記的限制

<merge /> 非常好用,但有兩條硬性限制:

· <merge /> 只能作為 XML 佈局的根標籤使用
· 當 Inflate 一個以 <merge /> 開頭的佈局檔時,必須明確指定父 ViewGroup,且 attachToRoot 要設為 true(可參考 inflate(int, android.view.ViewGroup, boolean) 方法)。

最終成果如下:

以上就是本文的全部內容,希望對大家的學習有所幫助,也歡迎持續關注本站。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved