Android中使用include標(biāo)簽和merge標(biāo)簽重復(fù)使用布局
盡管Android提供了各種組件來(lái)實(shí)現(xiàn)小而可復(fù)用的交互元素,你也可能因?yàn)椴季中枰獜?fù)用一個(gè)大組件。為了高效復(fù)用完整布局,你可以使用<include/>和<merge/>標(biāo)簽嵌入另一個(gè)布局到當(dāng)前布局。所以當(dāng)你通過(guò)寫一個(gè)自定義視圖創(chuàng)建獨(dú)立UI組件,你可以放到一個(gè)布局文件里,這樣更容易復(fù)用。
復(fù)用布局因?yàn)槠湓试S你創(chuàng)建可復(fù)用的復(fù)雜布局而顯得非常強(qiáng)大。如,一個(gè) 是/否 按鈕面板,或帶描述文本的自定義進(jìn)度條。這同樣意味著,應(yīng)用里多個(gè)布局里共同的元素可以被提取出來(lái),獨(dú)立管理,然后插入到每個(gè)布局里。
創(chuàng)建可復(fù)用布局
如果你已經(jīng)知道哪個(gè)布局需要重用,就創(chuàng)建一個(gè)新的xml文件來(lái)定義布局。如,下面是一個(gè)來(lái)自G-Kenya代碼庫(kù)里定義標(biāo)題欄的布局,它可以被插到每個(gè)Activity里:
<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>
根視圖應(yīng)該剛好和你在其他要插入這個(gè)視圖的視圖里相應(yīng)位置一樣。
使用<include/>標(biāo)簽
在你要添加可復(fù)用布局的布局里,添加<include/>標(biāo)簽。下面是添加標(biāo)題欄:
<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>
你同樣可以覆蓋所有的布局參數(shù)(android:layout_*屬性)
<include android:id=”@+id/news_title”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
layout=”@layout/title”/>
可是,如果你要用include標(biāo)簽覆蓋布局屬性,為了讓其他屬性生效,就必須覆蓋android:layout_height和android:layout_width。
使用<merge/>標(biāo)簽
<merge/>標(biāo)簽幫助你排除把一個(gè)布局插入到另一個(gè)布局時(shí)產(chǎn)生的多余的View Group.如,你的被復(fù)用布局是一個(gè)垂直的線性布局,包含兩個(gè)子視圖,當(dāng)它作為一個(gè)被復(fù)用的元素被插入到另一個(gè)垂直的線性布局時(shí),結(jié)果就是一個(gè)垂直的LinearLayout里包含一個(gè)垂直的LinearLayout。這個(gè)嵌套的布局并沒(méi)有實(shí)際意義,而且會(huì)讓UI性能變差。
為了避免插入類似冗余的View Group,你可以使用<merge/>標(biāo)簽標(biāo)簽作為可復(fù)用布局的根節(jié)點(diǎn),如:
<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>
現(xiàn)在,當(dāng)你使用include標(biāo)簽插入這個(gè)布局到另一個(gè)布局時(shí),系統(tǒng)會(huì)忽略merge標(biāo)簽,直接把兩個(gè)Button替換到include標(biāo)簽的位置。
- Android抽象布局——include、merge 、ViewStub詳解
- Android編程之include文件的使用方法
- android使用include調(diào)用內(nèi)部組件的方法
- 解析android中include標(biāo)簽的使用
- Android開(kāi)發(fā)之merge結(jié)合include優(yōu)化布局
- Android開(kāi)發(fā)技巧之ViewStub控件惰性裝載
- 深入分析Android ViewStub的應(yīng)用詳解
- Android組件ViewStub基本使用方法詳解
- Android布局技巧之include、merge與ViewStub標(biāo)簽的巧用
相關(guān)文章
Android實(shí)現(xiàn)仿魅族日歷首頁(yè)功能
這篇文章主要介紹了Android實(shí)現(xiàn)仿魅族日歷首頁(yè)功能的實(shí)現(xiàn)過(guò)程以及相關(guān)代碼講解分享,對(duì)此有興趣的朋友參考下。2018-02-02adb wireless進(jìn)行Android手機(jī)調(diào)試詳解
這篇文章給大家講解了在Android手機(jī)上使用adb wireless進(jìn)行調(diào)試的步驟以及問(wèn)題解決辦法,有需要的跟著學(xué)習(xí)下吧。2017-12-12TextView中URL等指定特殊字符串與點(diǎn)擊事件解析
這篇文章主要為大家詳細(xì)介紹了TextView中URL等指定特殊字符串與點(diǎn)擊事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Android Studio報(bào)錯(cuò)unable to access android sdk add-on list解決方案
這篇文章主要介紹了Android Studio報(bào)錯(cuò)unable to access android sdk add-on list解決方案,本文通過(guò)多種方式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Android亮度調(diào)節(jié)的幾種實(shí)現(xiàn)方法
本篇文章詳細(xì)介紹了Android亮度調(diào)節(jié)的幾種實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11Android Studio 利用Splash制作APP啟動(dòng)界面的方法
這篇文章主要介紹了Android Studio 利用Splash制作APP啟動(dòng)界面,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05Android實(shí)現(xiàn)中軸旋轉(zhuǎn)特效 Android制作別樣的圖片瀏覽器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)中軸旋轉(zhuǎn)特效,制作別樣的圖片瀏覽器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Android中HttpURLConnection與HttpClient的使用與封裝
這篇文章主要介紹了Android中HttpURLConnection與HttpClient的使用以及封裝方法,感興趣的小伙伴們可以參考一下2016-03-03