Android布局優化:include 、merge、ViewStub的詳細總結

一、include的用法以及注意點

在開發Android布局時,我們常將一些通用的視圖提取到一個單獨的layout文件中,然后使用<include>標簽在需要使用的其他layout布局文件中加載進來,比如我們自己App導航欄等。這樣,便于對相同視圖內容進行統一的控制管理,提高布局重用性。

下面我們以大部分項目中都有的頭部導航欄為例,說明一下include的使用,比如我們項目自己統一頭部導航欄,抽取布局如下:

titlebar.xml:

<?xml version="1.0"encoding="utf-8"?>

????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="返回按鈕" />??

????<TextView??

????????android:id="@+id/title"??

????????android:layout_width="wrap_content"??

????????android:layout_height="wrap_content"??

????????android:layout_centerInParent="true"??

????????android:text="提示文字"??

????????android:textSize="20sp" />??

????<Button??

????????android:id="@+id/close"??

????????android:layout_width="wrap_content"??

????????android:layout_height="wrap_content"??

????????android:layout_alignParentRight="true"??

????????android:layout_centerVertical="true"??

????????android:text="關閉按鈕" />??


很簡單,就是左右各一個按鈕,中間是一個提示文字。使用也比較簡單,如下:

activity_main.xml:

????xmlns:tools="http://schemas.android.com/tools"

????android:layout_width="match_parent"

????android:layout_height="match_parent"

????tools:context="${relativePackage}.${activityClass}" >

????<include

????????android:layout_width="match_parent"

????????android:layout_height="40dp"

????????layout="@layout/titlebar" />

????<Button

????????android:layout_width="wrap_content"

????????android:layout_height="wrap_content"

????????android:layout_centerHorizontal="true"

????????android:layout_centerVertical="true"

????????android:onClick="click"

????????android:text="點我。。。" />

include標簽使用還是很簡單的,主要通過layout屬性聲明要引入的布局即可。運行程序界面如下:

include標簽使用注意點:

1,標簽當中,可以重寫所有layout屬性的,如上面include中指定的layout屬性將會覆蓋掉titlebar中指定的layout屬性。

而非layout屬性則無法在標簽當中進行覆寫。另外需要注意的是,如果我們想要在標簽當中覆寫layout屬性,

必須要將layout_width和layout_height這兩個屬性也進行覆寫,否則覆寫效果將不會生效

2,一個xml布局文件有多個include標簽需要設置ID,才能找到相應子View的控件,否則只能找到第一個include的layout布局,以及該布局的控件。

3,如果我們給include所加載的layout布局的根容器設置了id屬性,也在include標簽中設置了id屬性,同時需要在代碼中獲取根容器的控件對象時,最好將這兩個id設置相同的名稱!否則,可能獲取不到根容器對象,即為null。

二、merge的用法以及注意點

merge標簽存在的意義是幫助include標簽排除多余的一層ViewGroup容器,減少view hierarchy的結構,提升UI渲染的性能。include標簽存在著一個不好的地方,可能會導致產生多余的布局嵌套。同樣通過一個小demo來說明:

比如項目中有一個公共的登錄按鈕布局,如下:

login.xml:

<?xml version="1.0"encoding="utf-8"?>

????android:layout_width="match_parent"

????android:layout_height="wrap_content"

????android:orientation="vertical" >


?????<Button?

????????android:layout_width="match_parent"??

????????android:layout_height="wrap_content"??

????????android:layout_marginLeft="20dp"??

????????android:layout_marginRight="20dp"??

????????android:text="登錄按鈕" />??


很簡單,就是一個登錄的Button。

項目中有登錄功能的UI界面如下:

activity_login.xml:

????xmlns:tools="http://schemas.android.com/tools"

????android:layout_width="match_parent"

????android:layout_height="match_parent"

????android:orientation="vertical"

????android:background="@android:color/holo_blue_light">

????<EditText???

????????android:layout_width="match_parent"??

????????android:layout_height="wrap_content"?

????????android:layout_marginLeft="20dp"??

????????android:layout_marginRight="20dp"??

????????android:layout_marginTop="40dp"??

????????android:hint="請輸入用戶名" />


????<include layout="@layout/login" />

同樣非常簡單,運行程序,如下:

看起來沒什么問題,其實不知不覺中我們多嵌套了一層布局。我們用工具查看一下此時布局結構:

除去系統布局,我們自己布局最外層是LinearLayout,然后兩個并列布局EditText與LinearLayout,在LinearLayout里面是Button登錄按鈕。

其實這種情況下:在主界面中,<include>標簽的parent ViewGroup與包含的layout根容器ViewGroup是相同的類型,這里都是LinearLayout,那么則可以將包含的layout根容器ViewGroup使用<merge>標簽代替,從而減少一層ViewGroup的嵌套,提升UI渲染性能。

這里我們把activity_login.xml修改如下:

<?xml version="1.0"encoding="utf-8"?>

?????<Button?

????????android:layout_width="match_parent"??

????????android:layout_height="wrap_content"??

????????android:layout_marginLeft="20dp"??

????????android:layout_marginRight="20dp"??

????????android:text="登錄按鈕" />??


重新運行程序UI和上面一樣效果,通過工具再次查看布局結構;

看到了吧,我們自己布局減少了一層嵌套,從而提升了UI的渲染速度。

merge標簽使用注意點:

1,根布局是FrameLayout且不需要設置background或padding等屬性,可以用merge代替,因為Activity的ContentView父元素就是FrameLayout,所以可以用merge消除只剩一個.

2,因為merge標簽并不是View,所以在通過LayoutInflate.inflate()方法渲染的時候,第二個參數必須指定一個父容器,且第三個參數必須為true,也就是必須為merge下的視圖指定一個父親節點.由于merge不是View所以****對merge標簽設置的所有屬性都是無效的.

LayoutInflate中源碼體現:

publicViewinflate(XmlPullParser parser, @Nullable ViewGroup root,booleanattachToRoot){synchronized(mConstructorArgs) {? ? ? ? ? ? ? ? ? ? ? ? ...if(TAG_MERGE.equals(name)) {if(root ==null|| !attachToRoot) {thrownewInflateException("<merge /> can be used only with a valid "+"ViewGroup root and attachToRoot=true");? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? rInflate(parser, root, inflaterContext, attrs,false);? ? ? ? ? ? ? ? }? ? ? ? ? ? ...? ? ? ? }? ? }

3,merge標簽必須使用在根布局,并且ViewStub標簽中的layout布局不能使用merge標簽.

三、ViewStub的用法以及注意點

ViewStub也可以用來加載布局文件,但與include標簽完全不同。ViewStub是一個不可見的View類,用于在運行時按需懶加載資源,只有在代碼中調用了viewStub.inflate()或者viewStub.setVisible(View.visible)方法時才內容才變得可見。這里需要注意的一點是,當ViewStub被inflate到parent時,ViewStub就被remove掉了,即當前view hierarchy中不再存在ViewStub,而是使用對應的layout視圖代替。

同樣我們通過一個小demo說明一下,比如我們需要保存一個用戶信息,用戶名是必須保存的,但是其余信息是不必要的,這是其余信息就可以一開始不顯示出來,用戶想輸入的時候在現實出來。

其余信息布局如下:

otherinfo.xml:

<?xml version="1.0"encoding="utf-8"?>

????android:layout_width="match_parent"

????android:orientation="vertical"

????android:layout_height="wrap_content" >

????<EditText

????????android:id="@+id/weichat_id"

????????android:layout_width="match_parent"

????????android:layout_height="wrap_content"

????????android:layout_marginLeft="20dp"

????????android:layout_marginRight="20dp"

????????android:layout_marginTop="10dp"

????????android:hint="請輸入微信號" />

????<EditText

????????android:id="@+id/address_id"

????????android:layout_width="match_parent"

????????android:layout_height="wrap_content"

????????android:layout_marginLeft="20dp"

????????android:layout_marginRight="20dp"

????????android:layout_marginTop="10dp"

????????android:hint="請輸入家庭住址" />

很簡單,沒什么其余解釋的,主界面布局如下:

activity_main.xml:

????xmlns:tools="http://schemas.android.com/tools"

????android:layout_width="match_parent"

????android:layout_height="match_parent"

????android:background="@android:color/holo_blue_light"

????android:orientation="vertical" >

????<EditText

????????android:layout_width="match_parent"

????????android:layout_height="wrap_content"

????????android:layout_marginLeft="20dp"

????????android:layout_marginRight="20dp"

????????android:layout_marginTop="40dp"

????????android:hint="請輸入用戶名" />

????<ViewStub

????????android:id="@+id/viewstub"

????????android:layout_width="match_parent"

????????android:layout_height="wrap_content"

????????android:layout="@layout/otherinfo" />

????<Button

????????android:layout_width="match_parent"

????????android:layout_height="wrap_content"

????????android:onClick="show"

????????android:layout_marginLeft="20dp"

????????android:layout_marginRight="20dp"

????????android:layout_marginTop="10dp"

????????android:text="顯示" />

其余信息界面通過ViewStub引入進來,關于ViewStub主要屬性以及方法說明如下:

android:layout屬性

加載包含的layout布局文件;

android:inflatedId屬性

重寫包含的layout布局文件的根容器id;

inflate()方法

與setVisible(int)方法作用類似,都可以使內容得以顯示,只是inflate()會返回一個View對象,避免了額外使用findViewById()方法獲取layout視圖對象。

activity中代碼如下:

publicvoidshow(View view){//ViewStub stub = ((ViewStub) findViewById(R.id.viewstub));if(stub!=null){? ? ? ? ? ? View stubView = stub.inflate();? ? ? ? ? ? EditText address = (EditText) stubView.findViewById(R.id.address_id);? ? ? ? ? ? ? EditText wechatId = (EditText) stubView.findViewById(R.id.weichat_id);? ? ? ? ? }? ? }

好了,運行程序,一開始如下:

點擊顯示按鈕,UI如下:

ViewStub標簽使用注意點:

1,ViewStub標簽不支持merge標簽。因此這有可能導致加載出來的布局存在著多余的嵌套結構,具體如何去取舍就要根據各自的實際情況來決定了。

2,ViewStub的inflate只能被調用一次,第二次調用會拋出異常。

3,雖然ViewStub是不占用任何空間的,但是每個布局都必須要指定layout_width和layout_height屬性,否則運行就會報錯。

好了,以上就是個人對于include 、merge、ViewStub使用的總結,希望對你有用,即使已經掌握,希望讀完此文能溫故知新

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,119評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,382評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,038評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,853評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,616評論 6 408
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,112評論 1 323
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,192評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,355評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,869評論 1 334
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,727評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,928評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,467評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,165評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,570評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,813評論 1 282
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,585評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,892評論 2 372

推薦閱讀更多精彩內容