)Android裡merge和include標籤的使用

來源:互聯網
上載者:User

1.使用<include /> 標籤來重用layout代碼

如果在一個項目中需要用到相同的布局設計,可以通過<include /> 標籤來重用layout代碼,該標籤在android開發文檔中沒有相關的介紹。在android主屏程式中 用到了這個標籤:

 

 

[xhtml:showcolumns] view plaincopy

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. <com.android.launcher.Workspace   
  2.  android:id="@+id/workspace"    
  3.  android:layout_width="fill_parent"    
  4.  android:layout_height="fill_parent"    
  5.  launcher:defaultScreen="1">    
  6.   <include android:id="@+id/cell1" layout="@layout/workspace_screen" />   
  7.   <include android:id="@+id/cell2" layout="@layout/workspace_screen" />    
  8.   <include android:id="@+id/cell3"layout="@layout/workspace_screen" />  
  9. </com.android.launcher.Workspace>  

 

 

這樣可以多次引用一個布局片段而不用重複的複製、粘貼。通過include標籤也可以覆寫一些屬性的值,例如上面的樣本就覆寫了引用的layout中的id值。下面是另外一個樣本:

[c-sharp] view plaincopy

  1. <include android:layout_width="fill_parent"layout="@layout/image_holder" />  
  2. <include android:layout_width="256dip" layout="@layout/image_holder" />  

2.使用<merge /> 標籤來減少視圖層級結構

在Android layout檔案中需要一個頂級容器來容納其他的組件,而不能直接放置多個組件,例如如下的代碼:
[c-sharp] view plaincopy

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent">  
  4.   
  5.     <ImageView  
  6.         android:layout_width="fill_parent"  
  7.         android:layout_height="fill_parent"   
  8.         android:scaleType="center"  
  9.         android:src="@drawable/golden_gate" />  
  10.   
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="Golden Gate" />  
  15.   
  16. </FrameLayout>  
單獨將<merge />標籤做個介紹,是因為它在最佳化UI結構時起到很重要的作用。目的是通過刪減多餘或者額外的層級,從而最佳化整個Android Layout的結構。

3.將通過一個例子來瞭解這個標籤實際所產生的作用,這樣可以更直觀的瞭解<merge/>的用法。

 

建立一個簡單的Layout,其中包含兩個Views元素:ImageView和TextView 預設狀態下我們將這兩個元素放在FrameLayout中。其效果是在主視圖中全螢幕顯示一張圖片,之後將標題顯示在圖片上,並位於視圖的下方。以下是xml代碼:

 

[xhtml:showcolumns] view plaincopy

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent">  
  4.   
  5.     <ImageView  
  6.         android:layout_width="fill_parent"  
  7.         android:layout_height="fill_parent"   
  8.   
  9.         android:scaleType="center"  
  10.         android:src="@drawable/golden_gate" />  
  11.   
  12.     <TextView  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_marginBottom="20dip"  
  16.         android:layout_gravity="center_horizontal|bottom"  
  17.   
  18.         android:padding="12dip"  
  19.   
  20.         android:background="#AA000000"  
  21.         android:textColor="#ffffffff"  
  22.   
  23.         android:text="Golden Gate" />  
  24.   
  25. </FrameLayout>  

 

 

應用上邊的Layout啟動並執行視圖為:

 

啟動 tools> hierarchyviewer.bat工具查看當前UI結構視圖:

 

 

我們可以很明顯的看到由紅色線框所包含的結構出現了兩個framelayout節點,很明顯這兩個完全意義相同的節點造成了資源浪費(這裡可以提醒大家在開發工程中可以習慣性的通過hierarchyViewer查看當前UI資源的分配情況),那麼如何才能解決這種問題呢(就當前例子是如何去掉多餘的frameLayout節點)?這時候就要用到<merge />標籤來處理類似的問題了。我們將上邊xml代碼中的framLayout替換成merge:

 

[xhtml:showcolumns] view plaincopy

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. <merge xmlns:android="http://schemas.android.com/apk/res/android">  
  2.   
  3.     <ImageView  
  4.         android:layout_width="fill_parent"  
  5.         android:layout_height="fill_parent"   
  6.   
  7.         android:scaleType="center"  
  8.         android:src="@drawable/golden_gate" />  
  9.   
  10.     <TextView  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_marginBottom="20dip"  
  14.         android:layout_gravity="center_horizontal|bottom"  
  15.   
  16.         android:padding="12dip"  
  17.   
  18.         android:background="#AA000000"  
  19.         android:textColor="#ffffffff"  
  20.   
  21.         android:text="Golden Gate" />  
  22.   
  23. </merge>  

  

 

 

運行程式後在Emulator中顯示的效果是一樣的,可是通過hierarchyviewer查看的UI結構是有變化的,當初多餘的 FrameLayout節點被合并在一起了,或者可以理解為將merge標籤中的子集直接加到Activity的FrameLayout跟節點下(這裡需要提醒大家注意:所有的Activity視圖的根節點都是frameLayout)。如果你所建立的Layout並不是用framLayout作為根節點(而是應用LinerLayout等定義root標籤),就不能應用上邊的例子通過merge來最佳化UI結構。


 

4.除了上邊的例子外,meger還有另外一個用法

當應用Include或者ViewStub標籤從外部匯入xml結構時,可以將被匯入的xml用merge作為根節點表示,這樣當被嵌入父級結構中後可以很好的將它所包含的子集融合到父級結構中,而不會出現冗餘的節點。

另外有兩點需要特別注意:

  • <merge />只可以作為xml layout的根節點。
  • 當需要擴充的xml layout本身是由merge作為根節點的話,需要將被匯入的xml layout置於 viewGroup中,同時需要設定attachToRoot為True。(更多說明請參見inflate()文檔) 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.