在布局優(yōu)化中,Androi的官方提到了這三種布局铆铆、蝶缀、,并介紹了這三種布局各有的優(yōu)勢(shì)薄货,下面也是簡單說一下他們的優(yōu)勢(shì)翁都,以及怎么使用,記下來權(quán)當(dāng)做筆記谅猾。
標(biāo)簽?zāi)軌蛑赜貌季治募唵蔚氖褂萌缦拢?/p>
[html]view plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
android:orientation="vertical"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background="@color/app_bg"
android:gravity="center_horizontal">
android:layout_height="wrap_content"
android:text="@string/hello"
android:padding="10dp"/>
...
1)標(biāo)簽可以使用單獨(dú)的layout屬性税娜,這個(gè)也是必須使用的坐搔。
2)可以使用其他屬性。標(biāo)簽若指定了ID屬性敬矩,而你的layout也定義了ID概行,則你的layout的ID會(huì)被覆蓋,解決方案弧岳。
3)在include標(biāo)簽中所有的android:layout_*都是有效的凳忙,前提是必須要寫layout_width和layout_height兩個(gè)屬性业踏。
4)布局中可以包含兩個(gè)相同的include標(biāo)簽,引用時(shí)可以使用如下方法解決(參考):
[html]view plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
Viewbookmarks_container_2=findViewById(R.id.bookmarks_favourite);
bookmarks_container_2.findViewById(R.id.bookmarks_list);
標(biāo)簽在UI的結(jié)構(gòu)優(yōu)化中起著非常重要的作用勤家,它可以刪減多余的層級(jí),優(yōu)化UI柳恐。多用于替換FrameLayout或者當(dāng)一個(gè)布局包含另一個(gè)時(shí)伐脖,標(biāo)簽消除視圖層次結(jié)構(gòu)中多余的視圖組。例如你的主布局文件是垂直布局胎撤,引入了一個(gè)垂直布局的include晓殊,這是如果include布局使用的LinearLayout就沒意義了断凶,使用的話反而減慢你的UI表現(xiàn)伤提。這時(shí)可以使用標(biāo)簽優(yōu)化。
[html]view plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/add"/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/delete"/>
現(xiàn)在认烁,當(dāng)你添加該布局文件時(shí)(使用標(biāo)簽)肿男,系統(tǒng)忽略節(jié)點(diǎn)并且直接添加兩個(gè)Button。更多介紹可以參考《Android Layout Tricks #3: Optimize by merging》
標(biāo)簽最大的優(yōu)點(diǎn)是當(dāng)你需要時(shí)才會(huì)加載舶沛,使用他并不會(huì)影響UI初始化時(shí)的性能。各種不常用的布局想進(jìn)度條窗价、顯示錯(cuò)誤消息等可以使用標(biāo)簽如庭,以減少內(nèi)存使用量,加快渲染速度撼港。是一個(gè)不可見的坪它,大小為0的View。標(biāo)簽使用如下:
[html]view plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
android:id="@+id/stub_import"
android:inflatedId="@+id/panel_import"
android:layout="@layout/progress_overlay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
當(dāng)你想加載布局時(shí)帝牡,可以使用下面其中一種方法:
[java]view plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
((ViewStub)?findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);
//?or
View?importPanel?=?((ViewStub)?findViewById(R.id.stub_import)).inflate();
當(dāng)調(diào)用inflate()函數(shù)的時(shí)候往毡,ViewStub被引用的資源替代,并且返回引用的view靶溜。這樣程序可以直接得到引用的view而不用再次調(diào)用函數(shù)findViewById()來查找了开瞭。
注:ViewStub目前有個(gè)缺陷就是還不支持標(biāo)簽。
參考:http://blog.csdn.net/xyz_lmn/article/details/14524567
http://blog.csdn.net/chenlaic/article/details/6090069
http://www.tuicool.com/articles/jyyUV33