android之merge布局

來源:互聯網
上載者:User

標籤:

<merge />標籤閃亮登場了。當LayoutInflater遇到這個標籤時,它會跳過它,並將<merge />內的元素添加到<merge />的父元素裡。

用<merge />來替換FrameLayout XML布局:

<merge xmlns:android="http://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時使用<merge />。

讓我們看一個使用<merge />建立一個新View的例子——OkCancelBar,包含兩個按鈕,並可以設定按鈕標籤。下面的XML用於在一個圖片上顯示自訂的View:

<merge    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:okCancelBar="http://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檔案中定義,通過LayoutInflate類匯入。如下面的程式碼片段所示,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();    }}

兩個按鈕的定義如下面的XML所示。正如你所看到的,我們使用<merge />標籤直接添加兩個按鈕到OkCancelBar。

每個按鈕都是從外部相同的XML布局檔案包含進來的,便於維護;我們只是簡單地重寫它們的id:

<merge xmlns:android="http://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)方法)。

android之merge布局

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.