include、ViewStub、merge優化佈局標籤
前言
在寫Android的xml佈局時,用好 include
、ViewStub
、merge
這三個標籤,可以是我們的xml更加簡潔、高效。
include
按照官方的意思,include就是為了解決重複定義相同佈局的問題。
相當於Java程式碼中將相同的部分抽取出來,然後複用,需要的時候引入它即可,而不必每次都自己寫一遍。
舉例說明:
一個公共佈局檔案 my_layout.xml
(這個佈局後面例子也會用到):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button"/> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView"/> </LinearLayout>
使用這個公共佈局檔案:
<?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 android:id="@+id/include_layout" layout="@layout/my_layout" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
注意事項:
使用 include 最常見的問題就是 findViewById 時候出現
NullPointerException 。
這個問題出現的前提是在 <include /> 中設定了id,被 include 進來的佈局的根元素也設定了id。
那麼這時使用被 include 進來的根元素id進行 findViewById 就會出現NullPointerException。
在上述例子中:
findViewById(R.id.include_layout);// 正常 findViewById(R.id.linearLayout);// 會出現NullPointerException findViewById(R.id.button);// 正常 findViewById(R.id.textView); // 正常
ViewStub
ViewStub
就是一個寬高都為0的一個View,它預設是不可見的。
只有通過呼叫 setVisibility()
函式或者 Inflate()
函式才會將其要裝載的目標佈局給加載出來,從而達到延遲載入的效果。
在ViewStub
佈局可顯示之前,系統不會消耗資源去例項化裡面的佈局,可以節省系統資源消耗。
設定 ViewStub 中延時載入的佈局有兩種方式:
- 在xml中使用
android:layout
屬性來設定。 - 在程式碼中使用
ViewStub.setLayoutResource(res);
來設定。
使ViewStub中佈局顯示出來也有兩種方法:
- 呼叫
ViewStub.setVisibility(View.VISIBLE);
。 - 呼叫
ViewStub.inflate();
。
這兩個方法本質上都是呼叫ViewStub.inflate();
來實現佈局的載入顯示。
舉例說明:
<ViewStub
android:id="@+id/view_stub"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:layout="@layout/my_layout"
android:inflatedId="@+id/view_stub_inflated_id"/>
// my_layout 佈局檔案就是上文中的 my_layout.xml
// 第一種使用方法:
//使用android:layout="@layout/my_layout"設定佈局
ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub);
//設定setVisibility,使佈局檔案例項化
viewStub .setVisibility(View.VISIBLE);
// 通過ViewStub的xml中的屬性 inflatedId 來獲取View
LinearLayout linearLayout =(LinearLayout) findViewById(R.id.view_stub_inflated_id);
if ( viewStub.getVisibility() == View.VISIBLE ) {
// 已經載入成功
}
// 第二種使用方法:
ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub);
viewStub.setLayoutResource(R.layout.my_layout);
//使用 inflate,使佈局檔案例項化
LinearLayout linearLayout= (LinearLayout)viewStub.inflate();
if (linearLayout!= null){
//已經載入成功
}
merge
merge
它可以刪減多餘的層級,優化UI。
例如你的主佈局檔案是垂直的LinearLayout,這時使用include將 my_layout.xml 引入進來。
新佈局也是垂直的LinearLayout,那麼這個新的LinearLayout就沒有任何意義了。使用的話反而增加反應時間。這時可以使用<merge/>標籤優化。
merge 原理就是在解析xml時候,如果是 <merge/> 標籤,那麼直接將其中的子元素新增到merge 標籤parent中,這樣就保證了不會引入額外的層級
示例如下 :
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"/>
</merge>
作者:Pan_大寶
連結:http://www.jianshu.com/p/354fb8a42ad8
來源:簡書
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。