前言
在寫Android的xml布局時包帚,用好 include
渔期、ViewStub
、merge
這三個標(biāo)簽,可以是我們的xml更加簡潔疯趟、高效拘哨。
include
按照官方的意思,include就是為了解決重復(fù)定義相同布局的問題信峻。
相當(dāng)于Java代碼中將相同的部分抽取出來倦青,然后復(fù)用,需要的時候引入它即可站欺,而不必每次都自己寫一遍姨夹。
舉例說明:
一個公共布局文件 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 時候出現(xiàn) NullPointerException 。
這個問題出現(xiàn)的前提是在 <include /> 中設(shè)置了id矾策,被 include 進(jìn)來的布局的根元素也設(shè)置了id磷账。
那么這時使用被 include 進(jìn)來的根元素id進(jìn)行 findViewById 就會出現(xiàn)NullPointerException。
在上述例子中:
findViewById(R.id.include_layout);// 正常
findViewById(R.id.linearLayout);// 會出現(xiàn)NullPointerException
findViewById(R.id.button);// 正常
findViewById(R.id.textView); // 正常
ViewStub
ViewStub
就是一個寬高都為0的一個View贾虽,它默認(rèn)是不可見的逃糟。
只有通過調(diào)用 setVisibility()
函數(shù)或者 Inflate()
函數(shù)才會將其要裝載的目標(biāo)布局給加載出來,從而達(dá)到延遲加載的效果蓬豁。
在ViewStub
布局可顯示之前绰咽,系統(tǒng)不會消耗資源去實例化里面的布局,可以節(jié)省系統(tǒng)資源消耗地粪。
設(shè)置 ViewStub 中延時加載的布局有兩種方式:
- 在xml中使用
android:layout
屬性來設(shè)置取募。 - 在代碼中使用
ViewStub.setLayoutResource(res);
來設(shè)置。
使ViewStub中布局顯示出來也有兩種方法:
- 調(diào)用
ViewStub.setVisibility(View.VISIBLE);
蟆技。 - 調(diào)用
ViewStub.inflate();
玩敏。
這兩個方法本質(zhì)上都是調(diào)用ViewStub.inflate();
來實現(xiàn)布局的加載顯示。
舉例說明:
<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"設(shè)置布局
ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub);
//設(shè)置setVisibility质礼,使布局文件實例化
viewStub .setVisibility(View.VISIBLE);
// 通過ViewStub的xml中的屬性 inflatedId 來獲取View
LinearLayout linearLayout =(LinearLayout) findViewById(R.id.view_stub_inflated_id);
if ( viewStub.getVisibility() == View.VISIBLE ) {
// 已經(jīng)加載成功
}
// 第二種使用方法:
ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub);
viewStub.setLayoutResource(R.layout.my_layout);
//使用 inflate旺聚,使布局文件實例化
LinearLayout linearLayout= (LinearLayout)viewStub.inflate();
if (linearLayout!= null){
//已經(jīng)加載成功
}
merge
merge
它可以刪減多余的層級,優(yōu)化UI眶蕉。
例如你的主布局文件是垂直的LinearLayout砰粹,這時使用include將 my_layout.xml 引入進(jìn)來。
新布局也是垂直的LinearLayout造挽,那么這個新的LinearLayout就沒有任何意義了碱璃。使用的話反而增加反應(yīng)時間。這時可以使用<merge/>標(biāo)簽優(yōu)化刽宪。
merge 原理就是在解析xml時候厘贼,如果是 <merge/> 標(biāo)簽,那么直接將其中的子元素添加到merge 標(biāo)簽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>