include與merge標(biāo)簽使用

Android include與merge標(biāo)簽使用詳解

  1. 簡(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í)只需修改一處即可。

  2. 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>

效果圖如下:


image.png

如此就可以將這個(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)簽。

  1. 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ō)明 )。

  1. 未解決

在布局優(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”,就可以開始分析。還未使用。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • include和merge標(biāo)簽的作用是實(shí)現(xiàn)布局文件的重用。使用include和merge標(biāo)簽將一個(gè)布局嵌入到另一個(gè)...
    3Q竹林閱讀 1,893評(píng)論 0 1
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,062評(píng)論 25 709
  • 尊重原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處:http://blog.csdn.net/a740169405/article/deta...
    良秋閱讀 5,503評(píng)論 1 6
  • Unit One P32 〔TITLE〕College is not an Ivory Tower 〔TOPIC〕...
    淺夏憶汐h閱讀 435評(píng)論 1 1
  • 焦點(diǎn)講師四期 翟沖【原創(chuàng)】分享第385天 2018.01.17 下午慌慌張張來(lái)到學(xué)校,先到班內(nèi)招招,把學(xué)生安...
    春暖花開zc閱讀 363評(píng)論 0 0

友情鏈接更多精彩內(nèi)容