一、include的用法以及注意點
官網(wǎng)文檔:通過 <include> 重復(fù)使用布局
include標(biāo)簽是用來解決布局復(fù)用的沉填。在開發(fā)Android布局時疗隶,我們常將一些通用的視圖提取到一個單獨的layout文件中,然后使用標(biāo)簽在需要使用的其他layout布局文件中加載進來翼闹。這樣斑鼻,便于對相同視圖內(nèi)容進行統(tǒng)一的控制管理,提高布局重用性猎荠。
下面我們以頭部導(dǎo)航欄為例坚弱,抽取布局如下:
title.xml:
<?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" >
<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" />
</RelativeLayout>
使用也比較簡單蜀备,如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include
android:layout_width="match_parent"
android:layout_height="40dp"
layout="@layout/title" />
</RelativeLayout>
include標(biāo)簽使用注意點:
1.標(biāo)簽當(dāng)中,可以重寫所有l(wèi)ayout屬性的荒叶,如上面include中指定的layout屬性將會覆蓋掉title中指定的layout屬性琼掠。 而非layout屬性則無法在標(biāo)簽當(dāng)中進行覆寫。另外需要注意的是停撞,如果我們想要在標(biāo)簽當(dāng)中覆寫layout屬性瓷蛙, 必須要將layout_width和layout_height這兩個屬性也進行覆寫,否則覆寫效果將不會生效戈毒。
2.一個xml布局文件有多個include標(biāo)簽需要設(shè)置ID,才能找到相應(yīng)子View的控件,否則只能找到第一個include的layout布局,以及該布局的控件艰猬。
3.如果我們給include所加載的layout布局的根容器設(shè)置了id屬性,也在include標(biāo)簽中設(shè)置了id屬性埋市,同時需要在代碼中獲取根容器的控件對象時冠桃,最好將這兩個id設(shè)置相同的名稱!否則道宅,可能獲取不到根容器對象食听,即為null。
二污茵、merge的用法以及注意點
在一個布局中添加另一個布局時樱报,<merge>
標(biāo)記有助于消除視圖層次結(jié)構(gòu)中的冗余視圖組。<merge>
的一個用例是泞当,通過擴展 ViewGroup
實現(xiàn)自定義視圖迹蛤。
例如,如果您的主布局是一個垂直 LinearLayout襟士,其中兩個連續(xù)視圖可以在多個布局中重復(fù)使用盗飒,那么放置這兩個視圖的可重復(fù)使用布局需要有自己的根視圖。不過陋桂,如果使用另一個 LinearLayout
作為可重復(fù)使用的布局的根視圖逆趣,則會導(dǎo)致垂直 LinearLayout
內(nèi)出現(xiàn)垂直 LinearLayout
。嵌套的 LinearLayout
沒有實際用處嗜历,而且會降低界面性能宣渗。
相反,您可以擴展 LinearLayout
以創(chuàng)建自定義視圖秸脱,并使用布局 XML 來描述其子視圖落包。XML 中的頂部標(biāo)記是 <merge>
部蛇,而不是 LinearLayout
摊唇,如以下示例所示:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/add"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/delete"/>
</merge>
merge標(biāo)簽使用注意點:
1.根布局是FrameLayout且不需要設(shè)置background或padding等屬性,可以用merge代替,因為Activity的ContentView父元素就是FrameLayout,所以可以用merge消除只剩一個.
2.因為merge標(biāo)簽并不是View,所以在通過LayoutInflate.inflate()方法渲染的時候,第二個參數(shù)必須指定一個父容器,且第三個參數(shù)必須為true,也就是必須為merge下的視圖指定一個父親節(jié)點.由于merge不是View所以****對merge標(biāo)簽設(shè)置的所有屬性都是無效的.
3.merge標(biāo)簽必須使用在根布局,并且ViewStub標(biāo)簽中的layout布局不能使用merge標(biāo)簽.
三涯鲁、ViewStub的用法以及注意點
官網(wǎng)文檔:按需加載視圖
有時巷查,您的布局需要很少使用的復(fù)雜視圖有序。無論是作品詳情、進度指示器還是撤消消息岛请,您都可以通過僅在需要時加載這些視圖來減少內(nèi)存使用量并加快渲染速度旭寿。
如果您有應(yīng)用將來需要的復(fù)雜視圖,可以通過為復(fù)雜且很少使用的視圖定義 [ViewStub]
來延遲加載資源崇败。
假設(shè)您有一個布局盅称,希望在應(yīng)用的用戶體驗歷程的后續(xù)階段加載:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv"
android:src="@drawable/logo"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
您可以使用以下 ViewStub 推遲加載。如需讓它顯示或加載任何內(nèi)容后室,您必須使其顯示引用的布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ViewStub
android:id="@+id/stub_import"
android:inflatedId="@+id/panel_import"
android:layout="@layout/heavy_layout_we_want_to_postpone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</FrameLayout>
activity中代碼如下:
public void show(View view){
//
ViewStub stub = ((ViewStub) findViewById(R.id.stub_import));
if(stub!=null){
View stubView = stub.inflate();
ImageView address = (ImageView ) stubView.findViewById(R.id.iv);
}
}
ViewStub標(biāo)簽使用注意點:
- ViewStub標(biāo)簽不支持merge標(biāo)簽缩膝。因此這有可能導(dǎo)致加載出來的布局存在著多余的嵌套結(jié)構(gòu),具體如何去取舍就要根據(jù)各自的實際情況來決定了岸霹。
- ViewStub的inflate只能被調(diào)用一次,第二次調(diào)用會拋出異常疾层。
- 雖然ViewStub是不占用任何空間的,但是每個布局都必須要指定layout_width和layout_height屬性贡避,否則運行就會報錯痛黎。
總結(jié)
1.使用include標(biāo)簽重用布局
2.使用merge標(biāo)簽避免冗余的布局嵌套
3.使用ViewStub實現(xiàn)按需加載