欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中使用include標(biāo)簽和merge標(biāo)簽重復(fù)使用布局

 更新時(shí)間:2014年06月26日 10:21:08   投稿:junjie  
這篇文章主要介紹了Android中使用include標(biāo)簽和merge標(biāo)簽重復(fù)使用布局,文中講解了創(chuàng)建可復(fù)用布局的例子以及include標(biāo)簽和merge標(biāo)簽使用例子,需要的朋友可以參考下

盡管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里:

復(fù)制代碼 代碼如下:

<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)題欄:

復(fù)制代碼 代碼如下:

<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_*屬性)

復(fù)制代碼 代碼如下:

<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),如:

復(fù)制代碼 代碼如下:

<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)簽的位置。

相關(guān)文章

最新評(píng)論