Android UI布局優(yōu)化之include聪舒、merge與ViewStub標(biāo)簽的巧用方法

前言

在開發(fā)中UI布局是我們都會遇到的問題,隨著UI越來越多虐急,布局的重復(fù)性箱残、復(fù)雜度也會隨之增長。

相信大家經(jīng)常聽到include止吁、merge被辑、ViewStub這樣的標(biāo)簽,官方也提到這三種布局可用于布局的優(yōu)化敬惦。今天就介紹下這三種布局的使用盼理,記錄下來,便于后續(xù)app中的使用俄删。

include布局重用

我們在開發(fā)App的時(shí)候宏怔,會看到有不同的頁面有相同的布局, 這話時(shí)候我們就需要將這些布局提前出來單獨(dú)放在一個(gè)layout文件里畴椰,在使用<include 標(biāo)簽引入到相應(yīng)的頁面布局文件里臊诊,主要通過include的layout屬性引用。

舉個(gè)例子

include的布局:
<?xml version="1.0" encoding="utf-8"? 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/container"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" 

 <TextView
 android:id="@+id/tv1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="這里是來自include布局" / 

</RelativeLayout 

activity的布局:

<?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" 

 <TextView
 android:id="@+id/tv2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="以下的內(nèi)容來自include標(biāo)簽" / 

 <include
 android:id="@+id/container"
 layout="@layout/include_layout"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/tv"
 android:layout_marginTop="10dp" / 
</RelativeLayout 

這個(gè)標(biāo)簽在日常工作使用還是很常見的斜脂。這里有幾點(diǎn)需要注意下:

1抓艳、如果給include標(biāo)簽 和 include所加載的布局 都添加id的話,那么id要保持一致秽褒,如例子中都是container壶硅,否則是在代碼中獲取不到RelativeLayout容器的。 當(dāng)然我們可以避免這樣的問題销斟,只需要給其中一項(xiàng)添加id屬性就可以庐椒。

2、include布局里元素的id 要和 include所在頁面布局里的其他元素id 不同蚂踊,如例子中的兩個(gè)textview约谈,如果把id設(shè)置相同了,程序運(yùn)行起來并不會報(bào)錯(cuò),但是textview的賦值只會賦值給其中的一個(gè)棱诱。

3笼吟、如果需要給include標(biāo)簽設(shè)置位置屬性的話放妈,如例子中的layout_below、layout_marginTop,這時(shí)候 必須 同時(shí)設(shè)置include標(biāo)簽的寬高屬性layout_width潜必、layout_height,否則編譯器是會報(bào)錯(cuò)的便斥。一般情況不需要設(shè)置include的其他屬性国夜,直接加載布局文件 <include layout="@layout/...."/

4、布局中可以包含兩個(gè)相同的include標(biāo)簽,如下代碼所示 兩個(gè)include都加載layout="@layout/include_layout"

<?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" 

 <TextView
 android:id="@+id/tv"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="以下的內(nèi)容來自include標(biāo)簽" / 

 <include
 android:id="@+id/container"
 layout="@layout/include_layout"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/tv"
 android:layout_marginTop="10dp" / 

 <include
 android:id="@+id/container2"
 layout="@layout/include_layout"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="80dp" / 
</RelativeLayout 

可以設(shè)置不同include的id屬性厦凤,引用的時(shí)候如下可以正常顯示:

View view = findViewById(R.id.container2);
TextView textView = view.findViewById(R.id.tv);
textView.setText("這里是來自 第二個(gè) include布局");

merge減少視圖層級

merge標(biāo)簽可用于減少視圖層級來優(yōu)化布局鼻吮,可以配合include使用,如果include標(biāo)簽的父布局 和 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" 

 <TextView
 android:id="@+id/tv"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="以下的內(nèi)容不是來自merge標(biāo)簽" / 

 <include
 layout="@layout/merge_layout"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="20dp" / 
</LinearLayout 

先看沒有使用merge的:

<?xml version="1.0" encoding="utf-8"? 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" 

 <TextView
 android:id="@+id/tv"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="這里是不是來自merge布局" / 
</LinearLayout 

看下view層的結(jié)構(gòu):


image.png

再看使用了merge的:

<?xml version="1.0" encoding="utf-8"? 
<merge xmlns:android="http://schemas.android.com/apk/res/android" 

 <TextView
 android:id="@+id/tv"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="這里是來自merge布局" / 
</merge 

view層結(jié)構(gòu):


image.png

可以看到對比,減少了一層的LinearLayout的嵌套博烂,需要注意的是使用merge的布局香椎,在include的標(biāo)簽設(shè)置距離屬性沒有生效,可以將一些間距屬性設(shè)置到include布局里元素上禽篱,具體看項(xiàng)目需求使用士鸥。

ViewStub按需加載

按需加載 顧名思義需要的時(shí)候再去加載,不需要的時(shí)候可以不用加載谆级,節(jié)約內(nèi)存使用烤礁。通常情況我們會使用setVisibility方法來控制視圖的顯示和隱藏,但是這種情況視圖已經(jīng)加載了肥照。

比如app中頁面里某個(gè)布局只需要在特定的情況下才顯示脚仔,其余情況下可以不用加載顯示,這時(shí)候可以使用ViewStub舆绎。

layout屬性是需要加載布局

<?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" 

 <ViewStub
 android:id="@+id/viewstub"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="20dp"
 android:layout="@layout/viewstub_layout" / 
</LinearLayout 

需要注意的是 ViewStub的inflate()方法只能被調(diào)用一次鲤脏,一旦調(diào)用后,ViewStub將從視圖中移除吕朵,被對應(yīng)的layout布局取代猎醇,同時(shí)會保留ViewStub上設(shè)置的屬性效果。

ViewStub viewstub = findViewById(R.id.viewstub);
viewstub.inflate();

這篇關(guān)于include努溃、merge硫嘶、ViewStub的使用就介紹到這里了,具體使用情況還得視項(xiàng)目而定。

總結(jié)

以上就是這篇文章的全部內(nèi)容了梧税,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值沦疾,如果有疑問大家可以留言交流称近,謝謝大家對的支持。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末哮塞,一起剝皮案震驚了整個(gè)濱河市刨秆,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌忆畅,老刑警劉巖衡未,帶你破解...
    沈念sama閱讀 219,539評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異家凯,居然都是意外死亡眠屎,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評論 3 396
  • 文/潘曉璐 我一進(jìn)店門肆饶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人岖常,你說我怎么就攤上這事驯镊。” “怎么了竭鞍?”我有些...
    開封第一講書人閱讀 165,871評論 0 356
  • 文/不壞的土叔 我叫張陵板惑,是天一觀的道長。 經(jīng)常有香客問我偎快,道長冯乘,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,963評論 1 295
  • 正文 為了忘掉前任晒夹,我火速辦了婚禮裆馒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘丐怯。我一直安慰自己喷好,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評論 6 393
  • 文/花漫 我一把揭開白布读跷。 她就那樣靜靜地躺著梗搅,像睡著了一般。 火紅的嫁衣襯著肌膚如雪效览。 梳的紋絲不亂的頭發(fā)上无切,一...
    開封第一講書人閱讀 51,763評論 1 307
  • 那天,我揣著相機(jī)與錄音丐枉,去河邊找鬼哆键。 笑死,一個(gè)胖子當(dāng)著我的面吹牛瘦锹,可吹牛的內(nèi)容都是我干的洼哎。 我是一名探鬼主播烫映,決...
    沈念sama閱讀 40,468評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼噩峦!你這毒婦竟也來了锭沟?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤识补,失蹤者是張志新(化名)和其女友劉穎族淮,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體凭涂,經(jīng)...
    沈念sama閱讀 45,850評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡祝辣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了切油。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蝙斜。...
    茶點(diǎn)故事閱讀 40,144評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖澎胡,靈堂內(nèi)的尸體忽然破棺而出孕荠,到底是詐尸還是另有隱情,我是刑警寧澤攻谁,帶...
    沈念sama閱讀 35,823評論 5 346
  • 正文 年R本政府宣布稚伍,位于F島的核電站,受9級特大地震影響戚宦,放射性物質(zhì)發(fā)生泄漏个曙。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評論 3 331
  • 文/蒙蒙 一受楼、第九天 我趴在偏房一處隱蔽的房頂上張望垦搬。 院中可真熱鬧,春花似錦艳汽、人聲如沸悼沿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽糟趾。三九已至,卻和暖如春甚牲,著一層夾襖步出監(jiān)牢的瞬間义郑,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評論 1 272
  • 我被黑心中介騙來泰國打工丈钙, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留非驮,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,415評論 3 373
  • 正文 我出身青樓雏赦,卻偏偏與公主長得像劫笙,于是被迫代替她去往敵國和親芙扎。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評論 2 355

推薦閱讀更多精彩內(nèi)容