include、ViewStub、merge优化布局标签
前言
在寫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"><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Button"/><TextViewandroid: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"><includeandroid: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 中延時加載的布局有兩種方式:
使ViewStub中布局顯示出來也有兩種方法:
這兩個方法本質(zhì)上都是調(diào)用ViewStub.inflate();來實現(xiàn)布局的加載顯示。
舉例說明:
<ViewStubandroid: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"> <Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Button"/><TextViewandroid:id="@+id/textView"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="TextView"/> </merge>作者:Pan_大寶
鏈接:http://www.jianshu.com/p/354fb8a42ad8
來源:簡書
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
總結(jié)
以上是生活随笔為你收集整理的include、ViewStub、merge优化布局标签的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android path 详解
- 下一篇: 启动模式 和 任务栈