Android布局技巧之合并布局
我們已經(jīng)有文章向你描述如何使用<include />標(biāo)簽來重用和共享你的布局代碼。這篇文章將向你闡述<merge />標(biāo)簽的使用以及如何與<include />標(biāo)簽互補使用。
<merge />標(biāo)簽用于減少View樹的層次來優(yōu)化Android的布局。通過看一個例子,你就能很容易的理解這個標(biāo)簽?zāi)芙鉀Q的問題。下面的XML布局顯示一個圖片,并且有一個標(biāo)題位于其上方。這個結(jié)構(gòu)相當(dāng)?shù)暮唵?;FrameLayout里放置了一個ImageView,其上放置了一個TextView:
<FrameLayout xmlns:android="http://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>
布局渲染起來很漂亮,而且看不出有什么問題:
當(dāng)你使用HierarchyViewer工具來檢查時,你會發(fā)現(xiàn)事情變得很有趣。如果你仔細(xì)查看View樹,你將會注意到,我們在XML文件中定義的FrameLayout(藍(lán)色高亮顯示)是另一個FrameLayout唯一的子元素:
既然我們的FrameLayout和它的父元素有著相同的尺寸(歸功于fill_parent常量),并且也沒有定義任何的background,額外的padding或者gravity,所以它完全是無用的。我們所做的,只是讓UI變得更為復(fù)雜。怎樣我們才能擺脫這個FrameLayout呢?畢竟,XML文檔需要一個根標(biāo)簽且XML布局總是與相應(yīng)的View實例想對應(yīng)。
這時候,<merge />標(biāo)簽閃亮登場了。當(dāng)LayoutInflater遇到這個標(biāo)簽時,它會跳過它,并將<merge />內(nèi)的元素添加到<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作為它的根標(biāo)簽(舉例),那么你就不能使用這個技巧。<merge />在其它的一些場合也很有用的。例如,它與<include />標(biāo)簽結(jié)合起來就能表現(xiàn)得很完美。你還可以在創(chuàng)建一個自定義的組合View時使用<merge />。讓我們看一個使用<merge />創(chuàng)建一個新View的例子——OkCancelBar,包含兩個按鈕,并可以設(shè)置按鈕標(biāo)簽。下面的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類導(dǎo)入。如下面的代碼片段所示,R.layout.okcancelbar以O(shè)kCancelBar為父元素:
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 />標(biāo)簽直接添加兩個按鈕到OkCancelBar。每個按鈕都是從外部相同的XML布局文件包含進(jìn)來的,便于維護(hù);我們只是簡單地重寫它們的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>
我們創(chuàng)建了一個靈活且易于維護(hù)的自定義View,它有著高效的View層次:
<merge />標(biāo)簽極其有用。然而它也有以下兩個限制:
· <merge />只能作為XML布局的根標(biāo)簽使用
· 當(dāng)Inflate以<merge />開頭的布局文件時,必須指定一個父ViewGroup,并且必須設(shè)定attachToRoot為true(參看inflate(int, android.view.ViewGroup, Boolean)方法)。
效果圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 5.0中CoordinatorLayout的使用技巧
- CoordinatorLayout的使用如此簡單(Android)
- Android 使用CoordinatorLayout實現(xiàn)滾動標(biāo)題欄效果的實例
- Android CoordinatorLayout詳解及實例代碼
- Android Studio使用小技巧:布局預(yù)覽時填充數(shù)據(jù)
- Android中ListView Item布局優(yōu)化技巧
- Android布局技巧之創(chuàng)建高效布局
- Android布局技巧之創(chuàng)建可重用的UI組件
- Android中關(guān)于CoordinatorLayout的一些實用布局技巧
相關(guān)文章
Android搭建本地Tomcat服務(wù)器及相關(guān)配置
這篇文章主要介紹了Android搭建本地Tomcat服務(wù)器及相關(guān)配置,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07Android中的廣播(BroadCast)詳細(xì)介紹
這篇文章主要介紹了Android中的廣播(BroadCast)詳細(xì)介紹,本文講解了什么是廣播、廣播有什么用、實現(xiàn)廣播、動態(tài)注冊方式、配置文件方式等內(nèi)容,需要的朋友可以參考下2015-03-03Android使用AsyncQueryHandler實現(xiàn)獲取手機(jī)聯(lián)系人功能
這篇文章主要為大家詳細(xì)介紹了Android使用AsyncQueryHandler實現(xiàn)獲取手機(jī)聯(lián)系人功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07Android編程實現(xiàn)webview執(zhí)行l(wèi)oadUrl時隱藏鍵盤的workround效果
這篇文章主要介紹了Android編程實現(xiàn)webview執(zhí)行l(wèi)oadUrl時隱藏鍵盤的workround效果,較為詳細(xì)的分析了執(zhí)行l(wèi)oadUrl時隱藏鍵盤的workround具體步驟與兩種實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10Android有效獲取狀態(tài)欄(StatusBar)高度的方法
這篇文章主要介紹了Android有效獲取狀態(tài)欄(StatusBar)高度的方法,涉及Android針對狀態(tài)欄(StatusBar)屬性操作的相關(guān)技巧,需要的朋友可以參考下2016-08-08Android系統(tǒng)默認(rèn)對話框添加圖片功能
這篇文章主要介紹了Android系統(tǒng)默認(rèn)對話框添加圖片的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-01-01如何設(shè)置Android studio 3.0顯示光標(biāo)返回上一次瀏覽位置的箭頭圖標(biāo)
這篇文章主要介紹了如何設(shè)置Android studio 3.0顯示光標(biāo)返回上一次瀏覽位置的箭頭圖標(biāo) 很多朋友反映剛升級了Android studio 3.0,發(fā)現(xiàn)光標(biāo)返回上一次瀏覽位置的箭頭圖標(biāo)沒有了,下文給大家介紹的非常詳細(xì),需要的朋友可以參考下2017-11-11