Android include與merge標(biāo)簽使用詳解
簡(jiǎn)介
include和merge標(biāo)簽的作用是實(shí)現(xiàn)布局文件的重用。就是說(shuō),為了高效復(fù)用及整合布局,使布局輕便化,我們可以使用include和merge標(biāo)簽將一個(gè)布局嵌入到另一個(gè)布局中,或者說(shuō)將多個(gè)布局中的相同元素抽取出來(lái),獨(dú)立管理,再?gòu)?fù)用到各個(gè)布局中,便于統(tǒng)一的調(diào)整。
比如,一個(gè)應(yīng)用中的多個(gè)頁(yè)面都要用到統(tǒng)一樣式的標(biāo)題欄或底部導(dǎo)航欄,這時(shí)就可以將標(biāo)題欄或底部導(dǎo)航欄的布局抽取出來(lái),再以include標(biāo)簽形式嵌入到需要的布局中,而不是多次copy代碼,這樣在修改時(shí)只需修改一處即可。include標(biāo)簽的使用
將需要重用的布局寫在一個(gè)單獨(dú)的xml文件中,再使用include標(biāo)簽復(fù)用到其他布局中。
例如,下面是一個(gè)標(biāo)題欄的布局文件:
titlebar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="@dimen/base_action_bar_height"
android:background="@color/topic_green" >
<ImageView
android:id="@+id/tv_setting_title"
android:layout_width="100dip"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:src="@drawable/logo_for_temp_white" />
<TextView
android:id="@+id/tv_back"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/img_back"
android:drawableLeft="@drawable/avoscloud_feedback_thread_actionbar_back"
android:gravity="left|center_vertical"
android:text="返回"
android:textColor="@color/white"
android:textSize="@dimen/text_size_large" />
</RelativeLayout>
效果圖如下:

如此就可以將這個(gè)標(biāo)題欄直接復(fù)用到其他布局中了:
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_aty_choose_role"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background_for_temp"
android:orientation="vertical" >
<include layout="@layout/titlebar"/>
...
</LinearLayout >
如此,titlebar的內(nèi)容就可以嵌入到include標(biāo)簽所在的位置了。
需要注意的地方:
我們可在include標(biāo)簽中更改一些屬性的值,比如重新設(shè)置id,改變布局屬性(即android:layout_*屬性)等,如下代碼所示:
<include android:id=”@+id/news_title”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
layout="@layout/titlebar"/>
若include標(biāo)簽中重新指定id,那么其中的控件就不可當(dāng)成主xml(包含include標(biāo)簽的xml)中的控件來(lái)直接獲得了,必須先獲得include對(duì)應(yīng)的xml文件(就是titlebar.xml),再通過(guò)布局文件的findViewById方法來(lái)獲得其中控件。 當(dāng)然,若原布局設(shè)置了id屬性,會(huì)被覆蓋掉。
當(dāng)需要在include標(biāo)簽中改變布局屬性時(shí),為了讓其他屬性生效,就必須重寫android:layout_height和android:layout_width屬性,否則任何針對(duì)layout調(diào)整都是無(wú)效的。
include有一個(gè)缺點(diǎn)就是可能會(huì)產(chǎn)生多余的層級(jí),比如,被復(fù)用布局是一個(gè)垂直的LinearLayout布局,當(dāng)以include標(biāo)簽插入到另一個(gè)垂直的LinearLayout布局中時(shí),結(jié)果就是一個(gè)垂直的LinearLayout里包含一個(gè)垂直的LinearLayout,這個(gè)嵌套的布局并沒(méi)有實(shí)際意義,只會(huì)讓UI性能變差。這時(shí)就可以使用merge標(biāo)簽。
- merge標(biāo)簽的使用
merge標(biāo)簽可以自動(dòng)消除當(dāng)一個(gè)布局插入到另一個(gè)布局時(shí)產(chǎn)生的多余的View Group,也可用于替換FrameLayout。用法就是直接使用merge標(biāo)簽標(biāo)簽作為復(fù)用布局的根節(jié)點(diǎn),如下所示:
user.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="姓名"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="年齡"/>
</merge>
再使用include標(biāo)簽復(fù)用到其他布局中:
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_aty_choose_role"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include layout="@layout/user"/>
...
</LinearLayout >
這時(shí),系統(tǒng)會(huì)自動(dòng)忽略merge標(biāo)簽,直接把兩個(gè)Button替換到include標(biāo)簽的位置。
也就是說(shuō),include和merge是配合使用的。
需要注意的地方:
merge標(biāo)簽只能作為復(fù)用布局的root元素來(lái)使用。
使用它來(lái)inflate一個(gè)布局時(shí),必須指定一個(gè)ViewGroup實(shí)例作為其父元素并且設(shè)置attachToRoot屬性為true(參考 inflate(int, android.view.ViewGroup, boolean) 方法的說(shuō)明 )。
- 未解決
在布局優(yōu)化中,Android官方提到了三種方式include 、merge 、ViewStub ,最后一個(gè)還未詳細(xì)了解。
怎么查看UI層級(jí)?了解到可以使用tools里面的hierarchyviewer.bat查看布局層次,在啟動(dòng)模擬器啟動(dòng)所要分析的程序,再啟動(dòng)hierarchyviewer.bat,選擇模擬器以及該程序,點(diǎn)擊“Load View Hierarchy”,就可以開始分析。還未使用。