欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Android布局技巧之include、merge與ViewStub標(biāo)簽的巧用

前言

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比藤縣網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式藤縣網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋藤縣地區(qū)。費(fèi)用合理售后完善,十多年實(shí)體公司更值得信賴。

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

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

include布局重用

app開發(fā)過程中,會(huì)遇到不同頁(yè)面里有相同的布局,這時(shí)我們可以將這些通用的布局提取出來到一個(gè)單獨(dú)的layout文件里,再使用<include>標(biāo)簽引入到相應(yīng)的頁(yè)面布局文件里,主要通過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所在頁(yè)面布局里的其他元素id 不同,如例子中的兩個(gè)textview,如果把id設(shè)置相同了,程序運(yùn)行起來并不會(huì)報(bào)錯(cuò),但是textview的賦值只會(huì)賦值給其中的一個(gè)。

3、如果需要給include標(biāo)簽設(shè)置位置屬性的話,如例子中的layout_below、layout_marginTop,這時(shí)候 必須 同時(shí)設(shè)置include標(biāo)簽的寬高屬性layout_width、layout_height,否則編譯器是會(huì)報(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減少視圖層級(jí)

merge標(biāo)簽可用于減少視圖層級(jí)來優(yōu)化布局,可以配合include使用,如果include標(biāo)簽的父布局 和 include布局的根容器是相同類型的,那么根容器的可以使用merge代替。 

頁(yè)面布局

<?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): 

Android布局技巧之include、merge與ViewStub標(biāo)簽的巧用

再看使用了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): 

Android布局技巧之include、merge與ViewStub標(biāo)簽的巧用

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

ViewStub按需加載

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

比如app中頁(yè)面里某個(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將從視圖中移除,被對(duì)應(yīng)的layout布局取代,同時(shí)會(huì)保留ViewStub上設(shè)置的屬性效果。

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

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

最后附上github地址https://github.com/taixiang/include (本地下載)

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)創(chuàng)新互聯(lián)的支持。

當(dāng)前文章:Android布局技巧之include、merge與ViewStub標(biāo)簽的巧用
本文地址:http://aaarwkj.com/article42/igipec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、動(dòng)態(tài)網(wǎng)站響應(yīng)式網(wǎng)站、虛擬主機(jī)、ChatGPT、網(wǎng)站營(yíng)銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)
一个人看的视频天堂色| 成人在线免费黄色小说| 亚洲一区二区三区国色天香| 日韩精品伦理中文字幕| 亚洲av十八禁在线播放| 国产高清视频成人在线观看| 日韩一区精品视频一区二区| 国产免费很黄很色视频| 色哟国产传媒视频在线观看| 性生活免费在线观看视频| 婷婷激情六月中文字幕| 国产成人av三级在线观看| 亚洲视一区二区三区四区| 91在线国产手机视频| 欧美人与性禽动交情品| 亚洲av成人精品网站推荐| 99热在线播放精品观看| 日本一级a级黄免视频| 国产激情福利一区二区| 亚洲福利一区二区在线| 日韩精品在线观看你懂的| 五月婷婷少妇中文字幕| 欧美护士激情第一欧美精品| 我要看国产一级内射片| 欧美生活一区二区三区| 国产精品一区欧美精品| 国产精品推荐在线观看| 久久久这里只有精品99| 亚洲一区二区三区熟妇| 人妻中字幕出轨中文字幕| 欧美成人一区二区三区片| 国产一区二区黄色在线| 久久久久久成人亚洲| 久久综合亚洲鲁鲁五月天| 国产麻豆91在线视频| 午夜性生活免费在线观看| 国产在线观看国产精品| 亚洲熟女av综合网丁香| 免费亚洲老熟熟女熟女熟女| 久久久久国产综合精品| 伊人亚洲中文一区二区|