国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

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

2020-04-11 11:49:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

盡管Android提供了各種組件來(lái)實(shí)現(xiàn)小而可復(fù)用的交互元素,你也可能因?yàn)椴季中枰獜?fù)用一個(gè)大組件。為了高效復(fù)用完整布局,你可以使用<include/>和<merge/>標(biāo)簽嵌入另一個(gè)布局到當(dāng)前布局。所以當(dāng)你通過(guò)寫(xiě)一個(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)簽的位置。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 哈尔滨市| 周宁县| 云梦县| 航空| 雷州市| 甘德县| 屏东县| 嵩明县| 建湖县| 南漳县| 郁南县| 永寿县| 凤城市| 萝北县| 登封市| 凤阳县| 靖西县| 泰兴市| 鄂温| 广汉市| 内黄县| 鹤庆县| 莱阳市| 左贡县| 芜湖县| 清新县| 彰化市| 宜章县| 左贡县| 江西省| 桂东县| 凉城县| 桂平市| 庄河市| 临海市| 淳安县| 东兴市| 合川市| 喀什市| 河北省| 金华市|