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

Android中使用include標簽和merge標簽重復使用布局

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

盡管Android提供了各種組件來實現(xiàn)小而可復用的交互元素,你也可能因為布局需要復用一個大組件。為了高效復用完整布局,你可以使用<include/>和<merge/>標簽嵌入另一個布局到當前布局。所以當你通過寫一個自定義視圖創(chuàng)建獨立UI組件,你可以放到一個布局文件里,這樣更容易復用。

復用布局因為其允許你創(chuàng)建可復用的復雜布局而顯得非常強大。如,一個 是/否 按鈕面板,或帶描述文本的自定義進度條。這同樣意味著,應用里多個布局里共同的元素可以被提取出來,獨立管理,然后插入到每個布局里。

創(chuàng)建可復用布局

如果你已經(jīng)知道哪個布局需要重用,就創(chuàng)建一個新的xml文件來定義布局。如,下面是一個來自G-Kenya代碼庫里定義標題欄的布局,它可以被插到每個Activity里:

復制代碼 代碼如下:

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

根視圖應該剛好和你在其他要插入這個視圖的視圖里相應位置一樣。

使用<include/>標簽

在你要添加可復用布局的布局里,添加<include/>標簽。下面是添加標題欄:

復制代碼 代碼如下:

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

復制代碼 代碼如下:

<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/>標簽

<merge/>標簽幫助你排除把一個布局插入到另一個布局時產(chǎn)生的多余的View Group.如,你的被復用布局是一個垂直的線性布局,包含兩個子視圖,當它作為一個被復用的元素被插入到另一個垂直的線性布局時,結(jié)果就是一個垂直的LinearLayout里包含一個垂直的LinearLayout。這個嵌套的布局并沒有實際意義,而且會讓UI性能變差。

為了避免插入類似冗余的View Group,你可以使用<merge/>標簽標簽作為可復用布局的根節(jié)點,如:

復制代碼 代碼如下:

<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)在,當你使用include標簽插入這個布局到另一個布局時,系統(tǒng)會忽略merge標簽,直接把兩個Button替換到include標簽的位置。

相關(guān)文章

最新評論