Android Layout標籤之-viewStub,requestFocus,merge,include

 定義Android Layout(XML)時,有四個比較特別的標籤是非常重要的,其中有三個是與資源複用有關,分別是<viewStub/>, <requestFocus />, <merge /> and<include />。可是以往我們所接觸的案例或者官方文檔的例子都沒有着重去介紹這些標籤的重要性。

·      <viewStub  />: 此標籤可以使UI在特殊情況下,直觀效果類似於設置View的不可見性,但是其更大的(R)意義在於被這個標籤所包裹的Views在默認狀態下不會佔用任何內存空間。viewStub通過include從外部導入Views元素。

·  用法:通過android:layout來指定所包含的內容。默認情況下,ViewStub所包含的標籤都屬於visibility=GONEviewStub通過方法inflate()來召喚系統加載其內部的Views

·         <ViewStub android:id="@+id/stub"

·         android:inflatedId="@+id/subTree"

·         android:layout="@layout/mySubTree"

·         android:layout_width="120dip"

android:layout_height="40dip" />

·      <include />:可以通過這個標籤直接加載外部的xml到當前結構中,是複用UI資源的常用標籤。

·  用法:將需要複用xml文件路徑賦予include標籤的Layout屬性。

·         <include android:id="@+id/cell1" layout="@layout/ar01" />

<include android:layout_width="fill_parent" layout="@layout/ar02" />

·      <requestFocus />: 標籤用於指定屏幕內的焦點View

·  用法: 將標籤置於Views標籤內部 <!– Easy AdSenser V2.37 –><!– Post[count: 2] –>

·         <EditText id="@+id/text"

·                      android:layout_width="fill_parent"

·                      android:layout_height="wrap_content"

·                      android:layout_weight="0"

·                      android:paddingBottom="4">

·                <requestFocus />

</EditText>

·      <merge />:

單獨將<merge />標籤做個介紹,是因爲它在優化UI結構時起到很重要的作用。目的是通過刪減多餘或者額外的層級,從而優化整個Android Layout的結構。

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

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

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

 

    <ImageView

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

 

        android:scaleType="center"

        android:src="@drawable/golden_gate" />

 

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginBottom="20dip"

        android:layout_gravity="center_horizontal|bottom"

 

        android:padding="12dip"

 

        android:background="#AA000000"

        android:textColor="#ffffffff"

 

        android:text="Golden Gate" />

 

</FrameLayout>

應用上邊的Layout運行的視圖爲:

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


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

<!– Easy AdSenser V2.37 –><!– Post[count: 2] –>

<merge xmlns:android="http://schemas.android.com/apk/res/android">

 

    <ImageView

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

 

        android:scaleType="center"

        android:src="@drawable/golden_gate" />

 

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginBottom="20dip"

        android:layout_gravity="center_horizontal|bottom"

 

        android:padding="12dip"

 

        android:background="#AA000000"

        android:textColor="#ffffffff"

 

        android:text="Golden Gate" />

 

</merge>

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


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

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

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

·      <merge />只可以作爲xml layout的根節點。

·      當需要擴充的xml layout本身是由merge作爲根節點的話,需要將被導入的xml layout置於 viewGroup中,同時需要設置attachToRootTrue。(更多說明請參見inflate()文檔)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章