android layout include merge,Android 布局优化之include与merge
Android 官方提供了三個(gè)用來(lái)優(yōu)化布局的標(biāo)簽,分別是include、merge與ViewStub,其中ViewStub是動(dòng)態(tài)加載視圖到內(nèi)存,大家可以查閱:Android UI布局優(yōu)化之ViewStub
一、include布局重用:
在Android的應(yīng)用程序開(kāi)發(fā)中,標(biāo)題欄是必不可少的一個(gè)元素,大部分頁(yè)面都要用到,而且布局都是一樣的,這時(shí)候使用include標(biāo)簽就顯得極其的方便。使用時(shí)通常需要注意以下幾點(diǎn)。
include標(biāo)簽的layout_*屬性會(huì)替換掉被include視圖的根節(jié)點(diǎn)的對(duì)應(yīng)屬性。
include標(biāo)簽的id屬性會(huì)替換掉被include視圖的根節(jié)點(diǎn)id
一個(gè)布局文件中支持include多個(gè)視圖,但是這樣會(huì)導(dǎo)致獲取被include視圖內(nèi)的控件時(shí),
解決方法請(qǐng)參考:www.coboltforge.com/2012/05/tech-stuff-layout/
下面例子中,titlebar_layout.xml為標(biāo)題欄布局,而activity_main.xml為主界面布局,activity_setting.xml為設(shè)置頁(yè)面布局,這這兩個(gè)界面中都include了titlebar_layout.xml視圖。
titlebar_layout.xml:
titlebar_layout.xml
android:id="@+id/preference_activity_title_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dip"
android:background="@drawable/zns_activity_title_bg">
android:id="@+id/preference_activity_title_text"
android:layout_width="match_parent"
android:layout_height="45dip"
android:gravity="center"
android:text="123"
android:textColor="#ffffff"
android:textSize="18sp" />
android:id="@+id/preference_activity_title_image"
android:layout_width="30dip"
android:layout_height="25dip"
android:layout_gravity="center_vertical"
android:scaleType="fitCenter"
android:layout_marginLeft="5dip"
android:src="@drawable/common_menu_selector_white" />
主界面:
主界面
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="這是內(nèi)容區(qū)域"
android:gravity="center"
android:textSize="25sp"
android:textColor="#ffffff"/>
當(dāng)然,其他界面使用include同樣能包含該標(biāo)題欄。
一、通過(guò)merge減少視圖節(jié)點(diǎn):
merge翻譯成中文是合并的意思,在Android中通過(guò)使用merge能夠減少視圖的節(jié)點(diǎn)數(shù),
從而減少視圖在繪制過(guò)程消耗的時(shí)間,達(dá)到提高UI性能的效果。使用merge時(shí)通常需要注意以下幾點(diǎn):
merge必須放在布局文件的根節(jié)點(diǎn)上。
merge并不是一個(gè)ViewGroup,也不是一個(gè)View,它相當(dāng)于聲明了一些視圖,等待被添加。
merge標(biāo)簽被添加到A容器下,那么merge下的所有視圖將被添加到A容器下。
因?yàn)閙erge標(biāo)簽并不是View,所以在通過(guò)LayoutInflate.inflate方法渲染的時(shí)候, 第二個(gè)參數(shù)必須指定一個(gè)父容器,且第三個(gè)參數(shù)必須為true,也就是必須為merge下的視圖指定一個(gè)父親節(jié)點(diǎn)。
如果Activity的布局文件根節(jié)點(diǎn)是FrameLayout,可以替換為merge標(biāo)簽,這樣,執(zhí)行setContentView之后,會(huì)減少一層FrameLayout節(jié)點(diǎn)。
自定義View如果繼承LinearLayout,建議讓自定義View的布局文件根節(jié)點(diǎn)設(shè)置成merge,這樣能少一層結(jié)點(diǎn)。
因?yàn)閙erge不是View,所以對(duì)merge標(biāo)簽設(shè)置的所有屬性都是無(wú)效的。
其中第一點(diǎn),我們看看LayoutInflate類的源碼說(shuō)明:
} else if (TAG_MERGE.equals(name)) {
// 如果merge不是根節(jié)點(diǎn),報(bào)錯(cuò)
throw new InflateException(" must be the root element");
}
其中第三點(diǎn),常用在自定義View中遇到,附上系統(tǒng)LayoutInflate類,對(duì)于該現(xiàn)象的源碼:
if (TAG_MERGE.equals(name)) {
// 如果是merge標(biāo)簽,指定的root為空,或則attachToRoot為false,則拋出異常信息
if (root == null || !attachToRoot) {
throw new InflateException(" can be used only with a valid "
+ "ViewGroup root and attachToRoot=true");
}
rInflate(parser, root, attrs, false, false);
}
針對(duì)第五點(diǎn),做一下對(duì)比:
布局文件1:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:text="頂部Button" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="底部Button" />
效果1:
效果一
布局文件2:
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:text="頂部Button" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="底部Button" />
效果2:
效果二
我們可以看到,如果使用merge,明顯少了一個(gè)FrameLayout節(jié)點(diǎn),這也算一個(gè)視圖優(yōu)化技巧。
下面對(duì)第六條(自定義View如果繼承LinearLayout,建議讓自定義View的布局文件根節(jié)點(diǎn)設(shè)置成merge,這樣能少一層結(jié)點(diǎn))進(jìn)行分析:
先看看效果,就是一個(gè)線性布局,上下各一個(gè)TextView,看看使用merge和不使用merge的視圖節(jié)點(diǎn),
以及使用merge的時(shí)候layoutInflate類的注意點(diǎn)。
效果圖:
效果圖
第一種情況(不使用merge):
布局文件:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
android:background="#000000"
android:gravity="center"
android:text="第一個(gè)TextView"
android:textColor="#ffffff" />
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
android:background="#ffffff"
android:gravity="center"
android:text="第一個(gè)TextView"
android:textColor="#000000" />
代碼:
/**
* 自定義的View,豎直方向的LinearLayout
*/
public class MergeLayout extends LinearLayout {
public MergeLayout(Context context) {
super(context);
LayoutInflater.from(context).inflate(R.layout.merge_activity, this, true);
}
}
視圖樹(shù):
視圖樹(shù)一
我們發(fā)現(xiàn),MergeLayout這個(gè)自定義控件的下面并不是直接跟著兩個(gè)TextView,
而是多了一個(gè)LinearLayout。
第二種情況(使用merge):
注意因?yàn)闉閙erge標(biāo)簽的設(shè)置的屬性都不會(huì)生效,所以原來(lái)LinearLayout標(biāo)簽上的屬性需要轉(zhuǎn)移到j(luò)ava代碼中設(shè)置。
布局文件:
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
android:background="#000000"
android:gravity="center"
android:text="第一個(gè)TextView"
android:textColor="#ffffff" />
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
android:background="#ffffff"
android:gravity="center"
android:text="第一個(gè)TextView"
android:textColor="#000000" />
個(gè)人習(xí)慣在用merge的時(shí)候在旁邊標(biāo)明使用到的屬性,以防忘記。
java代碼中需要設(shè)置orientation屬性:
/**
* 自定義的View,豎直方向的LinearLayout
*/
public class MergeLayout extends LinearLayout {
public MergeLayout(Context context) {
super(context);
// 設(shè)置為數(shù)值方向的布局
setOrientation(VERTICAL);
LayoutInflater.from(context).inflate(R.layout.merge_activity, this, true);
}
}
再看看視圖樹(shù):
視圖樹(shù)二
我們發(fā)現(xiàn),LinearLayout節(jié)點(diǎn)被去掉了。但是最終顯示給用戶的界面卻是一樣的。
總結(jié)
1. 使用include標(biāo)簽可以增加布局的復(fù)用性,提高效率。
2. 使用merge標(biāo)簽可以減少視圖樹(shù)中的節(jié)點(diǎn)個(gè)數(shù),加快視圖的繪制,提高UI性能。
3. merge標(biāo)簽的使用,看上去一次只減少一個(gè)節(jié)點(diǎn),但是當(dāng)一個(gè)布局嵌套很復(fù)雜的時(shí)候,
節(jié)點(diǎn)的個(gè)數(shù)可能達(dá)到幾百個(gè),這個(gè)時(shí)候,如果每個(gè)地方都多一個(gè)節(jié)點(diǎn),視圖的繪制時(shí)間相應(yīng)的也就變長(zhǎng)了很多。
UI性能的優(yōu)化還有另外一個(gè)比較重要的知識(shí)點(diǎn)ViewStub,它是一個(gè)View,但是它幾乎不占用資源,
使用ViewStub能夠加快視圖的繪制,提高性能,關(guān)于ViewStub的知識(shí),大家可以參看博文:
Android UI布局優(yōu)化之ViewStub
總結(jié)
以上是生活随笔為你收集整理的android layout include merge,Android 布局优化之include与merge的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 4dda在linux中的意思,Evvai
- 下一篇: android 刷系统,安卓10的刷机教