Android布局優(yōu)化(1)

Include递胧、Merger橡庞、ViewStub用法

Include使用

?使用方式

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:gravity="center"
             android:orientation="vertical">
   <TextView
       android:id="@+id/id_1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
       <!--1温鸽、include的id會(huì)覆蓋重用布局跟id肥哎;2葫笼、可以嵌套使用-->
   <include
       android:id="@+id/id_2"
       layout="@layout/layout_1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_weight="0.5"/>
</LinearLayout>

?被重用布局:layout_1

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/id_include_layout"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:gravity="center"
             android:orientation="vertical">
   <TextView
     android:id="@+id/id_3"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
</LinearLayout>

?include標(biāo)簽說(shuō)名
?layout:必須使用屹逛,即被重用布局名稱(chēng)

Merger:減少視圖層級(jí)

?Merger消除視圖層次結(jié)構(gòu)中多余的層級(jí)础废。例如:當(dāng)前布局根為L(zhǎng)inearLayout,通過(guò)include引入一個(gè)重復(fù)的根布局為L(zhǎng)inearLayout罕模,此時(shí)就會(huì)增加視圖中的層級(jí)评腺,可以使用Merger代替重用布局中的根。如下列所示:

<?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/description"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="test" />

   <include layout="@layout/merger_layout" />
</LinearLayout>

?被Merger布局名稱(chēng):merger_layout

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

   <TextView
       android:id="@+id/description_merger"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="merger" />
</merge>

?總結(jié):通常Merger是配合include使用淑掌,減少布局層級(jí)

ViewStub:占位

?簡(jiǎn)介:當(dāng)需要時(shí)才會(huì)加載蒿讥,不會(huì)影響UI初始化性能。ViewStub為不可見(jiàn)抛腕,大小為0的View芋绸,需要在代碼中動(dòng)態(tài)動(dòng)態(tài)加載,通過(guò)setVisibility或者Inflate函數(shù)才會(huì)將布局顯示出來(lái)兽埃。ViewStub所要替代的layout文件中不能有<merge>標(biāo)簽以及ViewStub在加載完后會(huì)被移除侥钳,或者說(shuō)是被加載進(jìn)來(lái)的layout替換掉。

?使用方式:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ViewStub
        android:id="@+id/view_stub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inflatedId="@+id/inflate_id"
        android:layout="@layout/view_stub_layout" />

</LinearLayout>

?動(dòng)態(tài)加載布局:view_stub_layout.xml

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

    <TextView
        android:id="@+id/view_stub_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="view_stub_layout"/>
</LinearLayout>

?在代碼中使得ViewStub布局顯示出來(lái)

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //ViewStub visible或者inflate柄错,則ViewStub將從視圖框架中移除舷夺,只能visible或inflate一次
        //方式一
        View viewStub = findViewById(R.id.view_stub);
        viewStub.setVisibility(View.VISIBLE);

        //方式二
        ViewStub viewStubInflate = findViewById(R.id.view_stub);
        viewStubInflate.inflate();
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末苦酱,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子给猾,更是在濱河造成了極大的恐慌疫萤,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,039評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件敢伸,死亡現(xiàn)場(chǎng)離奇詭異扯饶,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)池颈,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)尾序,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人躯砰,你說(shuō)我怎么就攤上這事每币。” “怎么了琢歇?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,417評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵兰怠,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我李茫,道長(zhǎng)揭保,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,868評(píng)論 1 295
  • 正文 為了忘掉前任魄宏,我火速辦了婚禮秸侣,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘娜庇。我一直安慰自己塔次,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,892評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布名秀。 她就那樣靜靜地躺著励负,像睡著了一般。 火紅的嫁衣襯著肌膚如雪匕得。 梳的紋絲不亂的頭發(fā)上继榆,一...
    開(kāi)封第一講書(shū)人閱讀 51,692評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音汁掠,去河邊找鬼略吨。 笑死,一個(gè)胖子當(dāng)著我的面吹牛考阱,可吹牛的內(nèi)容都是我干的翠忠。 我是一名探鬼主播,決...
    沈念sama閱讀 40,416評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼乞榨,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼秽之!你這毒婦竟也來(lái)了当娱?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,326評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤考榨,失蹤者是張志新(化名)和其女友劉穎跨细,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體河质,經(jīng)...
    沈念sama閱讀 45,782評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡冀惭,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,957評(píng)論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了掀鹅。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片散休。...
    茶點(diǎn)故事閱讀 40,102評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖淫半,靈堂內(nèi)的尸體忽然破棺而出溃槐,到底是詐尸還是另有隱情,我是刑警寧澤科吭,帶...
    沈念sama閱讀 35,790評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站猴鲫,受9級(jí)特大地震影響对人,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜拂共,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,442評(píng)論 3 331
  • 文/蒙蒙 一牺弄、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧宜狐,春花似錦势告、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,996評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至俭驮,卻和暖如春回溺,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背混萝。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,113評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工遗遵, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人逸嘀。 一個(gè)月前我還...
    沈念sama閱讀 48,332評(píng)論 3 373
  • 正文 我出身青樓车要,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親崭倘。 傳聞我的和親對(duì)象是個(gè)殘疾皇子翼岁,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,044評(píng)論 2 355

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