Android佈局優化——include、merge和ViewStub標籤

1.include標籤

標籤描述:
可以允許在一個佈局當中引入另外一個佈局,實現佈局的複用,精簡佈局代碼。例子:Activity常用top欄

代碼實例
引入佈局:titlebar.xml

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent" >  
  
    <Button  
        android:id="@+id/back"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_centerVertical="true"  
        android:text="Back" />  
  
    <TextView  
        android:id="@+id/title"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_centerInParent="true"  
        android:text="Title"  
        android:textSize="20sp" />  
  
    <Button  
        android:id="@+id/done"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentRight="true"  
        android:layout_centerVertical="true"  
        android:text="Done" />  
</RelativeLayout>  

include使用

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical" >  
  
    <include layout="@layout/titlebar" />  
  
    ......  
  
</LinearLayout>  

2.merge標籤

標籤描述:
merge標籤是作爲include標籤的一種輔助擴展來使用,其主要作用是爲了防止在引用佈局文件時產生多餘的佈局嵌套。當include標籤引入一個帶merge標籤的佈局時,merge標籤內包含的內容會直接填充到include的位置,不會再添加任何額外的佈局結構。

代碼實例
帶merge標籤的佈局:merge_layout.xml

<?xml version="1.0" encoding="utf-8"?>  
<merge xmlns:android="http://schemas.android.com/apk/res/android">  
  
    <Button  
        android:id="@+id/ok"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:layout_marginLeft="20dp"  
        android:layout_marginRight="20dp"  
        android:text="OK" />  
  
    <Button  
        android:id="@+id/cancel"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:layout_marginLeft="20dp"  
        android:layout_marginRight="20dp"  
        android:layout_marginTop="10dp"  
        android:text="Cancel" />  
  
</merge>  

include引入merge標籤佈局:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical" >  
      
    <EditText  
        android:id="@+id/edit"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:layout_marginBottom="10dp"  
        android:layout_marginLeft="20dp"  
        android:layout_marginRight="20dp"  
        android:layout_marginTop="10dp"  
        android:hint="Edit something here" />  
      
    <include layout="@layout/merge_layout"/>  
  
</LinearLayout>  

3.ViewStub標籤

標籤描述:
ViewStub雖說也是View的一種,但是它沒有大小,沒有繪製功能,也不參與佈局,資源消耗非常低,將它放置在佈局當中基本可以認爲是完全不會影響性能的。其中的佈局只有在需要顯示時纔會加載,不顯示就不加載,所謂按需加載。

代碼實例
帶有ViewStub佈局的代碼:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical" >  
  
    <EditText  
        android:id="@+id/edit"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:layout_marginBottom="10dp"  
        android:layout_marginLeft="20dp"  
        android:layout_marginRight="20dp"  
        android:layout_marginTop="10dp"  
        android:hint="@string/edit_something_here" />  
  
    <Button  
        android:id="@+id/more"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_gravity="right"  
        android:layout_marginRight="20dp"  
        android:layout_marginBottom="10dp"  
        android:text="More" />  
      
    <ViewStub   
        android:id="@+id/view_stub"  
        android:layout="@layout/every_layout"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        />  
</LinearLayout>  

其中android:layout="@layout/every_layout" ,可以爲常見的任意佈局代碼。

ViewStub顯示代碼:

((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);  
// 或者
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();  

參考文檔

Android最佳性能實踐(四)——佈局優化技巧

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