W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
編寫:allenlsy - 原文:http://developer.android.com/training/improving-layouts/reusing-layouts.html
雖然 Android 提供很多小的可重用的交互組件,你仍然可能需要重用復(fù)雜一點(diǎn)的組件,這也許會(huì)用到 Layout。為了高效重用整個(gè)的 Layout,你可以使用 <include/>
和 <merge/>
標(biāo)簽把其他 Layout 嵌入當(dāng)前 Layout。
重用 Layout 非常強(qiáng)大,它讓你可以創(chuàng)建復(fù)雜的可重用 Layout。比如,一個(gè) yes/no 按鈕面板,或者帶有文字的自定義進(jìn)度條。這也意味著,任何在多個(gè) Layout 中重復(fù)出現(xiàn)的元素可以被提取出來,被單獨(dú)管理,再添加到 Layout 中。所以,雖然可以添加一個(gè)自定義 View 來實(shí)現(xiàn)單獨(dú)的 UI 組件,你可以更簡(jiǎn)單的直接重用某個(gè) Layout 文件。
如果你已經(jīng)知道你需要重用的 Layout,就先創(chuàng)建一個(gè)新的 XML 文件并定義 Layout 。比如,以下是一個(gè)來自 G-Kenya codelab 的 Layout,定義了一個(gè)需要添加到每個(gè) Activity 中的標(biāo)題欄(titlebar.xml):
<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>
根節(jié)點(diǎn) View 就是你想添加入的 Layout 類型。
<include>
標(biāo)簽使用 <include>
標(biāo)簽,可以在 Layout 中添加可重用的組件。比如,這里有一個(gè)來自 G-Kenya codelab 的 Layout 需要包含上面的那個(gè)標(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>
你也可以覆寫被添加的 Layout 的所有 Layout 參數(shù)(任何 android:layout_* 屬性),通過在 <include/>
中聲明他們來完成。比如:
<include android:id="@+id/news_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/title"/>
然而,如果你要在 <include>
中覆寫某些屬性,你必須先覆寫 android:layout_height
和 android:layout_width
。
<merge>
標(biāo)簽<merge />
標(biāo)簽在你嵌套 Layout 時(shí)取消了 UI 層級(jí)中冗余的 ViewGroup 。比如,如果你有一個(gè) Layout 是一個(gè)豎直方向的 LinearLayout,其中包含兩個(gè)連續(xù)的 View 可以在別的 Layout 中重用,那么你會(huì)做一個(gè) LinearLayout 來包含這兩個(gè) View ,以便重用。不過,當(dāng)使用一個(gè) LinearLayout 作為另一個(gè) LinearLayout 的根節(jié)點(diǎn)時(shí),這種嵌套 LinearLayout 的方式除了減慢你的 UI 性能外沒有任何意義。
為了避免這種情況,你可以用 <merge>
元素來替代可重用 Layout 的根節(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)你要將這個(gè) Layout 包含到另一個(gè) Layout 中時(shí)(并且使用了 <include/>
標(biāo)簽),系統(tǒng)會(huì)忽略 <merge>
標(biāo)簽,直接把兩個(gè) Button 放到 Layout 中 <include>
的所在位置。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: