android 佈局優化標籤、、

在佈局優化中,Android的官方提到了三種佈局標籤<include />、<merge />、<ViewStub />,並介紹了這三種佈局各有的優勢,下面也是簡單說一下他們的優勢,以及怎麼使用,記下來權當做筆記。


1、佈局重用<include />

<include />標籤能夠重用佈局文件,簡單的使用如下:


  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:orientation="vertical"   
  3.     android:layout_width=”match_parent”  
  4.     android:layout_height=”match_parent”  
  5.     android:background="@color/app_bg"  
  6.     android:gravity="center_horizontal">  
  7.   
  8.     <include layout="@layout/titlebar"/>  
  9.   
  10.     <TextView android:layout_width=”match_parent”  
  11.               android:layout_height="wrap_content"  
  12.               android:text="@string/hello"  
  13.               android:padding="10dp" />  
  14.   
  15.     ...  
  16.   
  17. </LinearLayout>  

    1)<include />標籤可以使用單獨的layout屬性,這個也是必須使用的。

    2)可以使用其他屬性。<include />標籤若指定了ID屬性,而你的layout也定義了ID,則你的layoutID會被覆蓋,解決方案

    3)在include標籤中所有的android:layout_*都是有效的,前提是必須要寫layout_widthlayout_height兩個屬性

    4)佈局中可以包含兩個相同的include標籤,引用時可以使用如下方法解決(參考):

  1. View bookmarks_container_2 = findViewById(R.id.bookmarks_favourite);   
  2.   
  3. bookmarks_container_2.findViewById(R.id.bookmarks_list);  


2、減少視圖層級<merge />

    <merge/>標籤在UI的結構優化中起着非常重要的作用,它可以刪減多餘的層級,優化UI。<merge/>多用於替換FrameLayout或者當一個佈局包含另一個時,<merge/>標籤消除視圖層次結構中多餘的視圖組。例如你的主佈局文件是垂直佈局,引入了一個垂直佈局的include,這是如果include佈局使用的LinearLayout就沒意義了,使用的話反而減慢你的UI表現。這時可以使用<merge/>標籤優化。

  1. <merge xmlns:android="http://schemas.android.com/apk/res/android">  
  2.   
  3.     <Button  
  4.         android:layout_width="fill_parent"   
  5.         android:layout_height="wrap_content"  
  6.         android:text="@string/add"/>  
  7.   
  8.     <Button  
  9.         android:layout_width="fill_parent"   
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/delete"/>  
  12.   
  13. </merge>  

     現在,當你添加該佈局文件時(使用<include />標籤),系統忽略<merge />節點並且直接添加兩個Button。更多<merge />介紹可以參考Android Layout Tricks #3: Optimize by merging


3、需要時使用<ViewStub />

    <ViewStub />標籤最大的優點是當你需要時纔會加載,使用他並不會影響UI初始化時的性能。各種不常用的佈局想進度條、顯示錯誤消息等可以使用<ViewStub />標籤,以減少內存使用量,加快渲染速度。<ViewStub />是一個不可見的,大小爲0View。<ViewStub />標籤使用如下:


  1. <ViewStub  
  2.     android:id="@+id/stub_import"  
  3.     android:inflatedId="@+id/panel_import"  
  4.     android:layout="@layout/progress_overlay"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="wrap_content"  
  7.     android:layout_gravity="bottom" />  


當你想加載佈局時,可以使用下面其中一種方法:


  1. ((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);  
  2. // or  
  3. View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();  


當調用inflate()函數的時候,ViewStub被引用的資源替代,並且返回引用的view 這樣程序可以直接得到引用的view而不用再次調用函數findViewById()來查找了。
ViewStub目前有個缺陷就是還不支持 <merge /> 標籤。


更多<ViewStub />標籤介紹可以參考《Android Layout Tricks #3: Optimize with stubs


參考:

http://developer.android.com/training/improving-layouts/reusing-layouts.html

http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html

http://developer.android.com/training/improving-layouts/optimizing-layout.html#Lint

http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html

http://developer.android.com/training/improving-layouts/loading-ondemand.html


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