當你在Application中建立複雜的佈局時,頁面的渲染過程也變得更加緩慢。
此時,咱們須要利用 <include />標籤(避免重複渲染)和 ViewStub類(延遲加載)來優化咱們的頁面。
1、利用<include />標籤來避免重複渲染
當咱們須要爲App中的每一個View都添加一個header或者footer時,你會怎麼作?
重複地複製粘貼能夠解決這個問題,但未免太繁雜。能夠試着使用<include />標籤:
第一種方式,在<include />標籤內指定width及height:
main.xml
- <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
- android:layout_width= "fill_parent"
- android:layout_height= "fill_parent" >
-
- <Button
- android:layout_width ="fill_parent"
- android:layout_height ="wrap_content"
- android:layout_gravity ="center_vertical"
- android:onClick ="onShowMap"
- android:text ="@string/show_map" />
-
- <include
- android:layout_width ="fill_parent"
- android:layout_height ="wrap_content"
- android:layout_alignParentBottom ="true"
- android:layout_marginBottom ="30dp"
- layout ="@layout/footer" />
-
- </RelativeLayout>
footer.xml
- <TextView xmlns:android = "http://schemas.android.com/apk/res/android"
- android:layout_width= "0dp"
- android:layout_height= "0dp"
- android:gravity= "center"
- android:text= "@string/footer_text" />
有個小細節須要注意,在footer.xml中,咱們將width及height都設爲0dp.目的是爲了配合<include/>標籤中對width及height的定義。
第二種方式,直接引用:
main.xml
- <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
- android:layout_width= "fill_parent"
- android:layout_height= "fill_parent" >
-
- <Button
- android:layout_width ="fill_parent"
- android:layout_height ="wrap_content"
- android:layout_gravity ="center_vertical"
- android:onClick ="onShowMap"
- android:text ="@string/show_map" />
-
- <include layout ="@layout/footer" />
-
- </RelativeLayout>
footer.xml
- <TextView xmlns:android = "http://schemas.android.com/apk/res/android"
- android:layout_width= "fill_parent"
- android:layout_height= "wrap_content"
- android:layout_alignParentBottom= "true"
- android:layout_marginBottom= "30dp"
- android:gravity= "center"
- android:text= "@string/footer_text" />
2、利用ViewStub類來延遲加載視圖
在設計視圖時,有時會考慮到某些視圖的可見性是依賴於用戶的操做或者運行設備的具體環境的。
此時你會如何設計?僅僅是改變View的visible屬性?
咱們先來看看ViewStub的介紹:
ViewStub是一個不可見、不佔空間(zero-sized)的控件,它能夠用來在運行時延遲加載視圖資源。只有當咱們將ViewStub的可見性設爲true,或者調用inflate()方法,它的視圖資源纔會被加載。
假設咱們設計的一個頁面中包含地圖View,試想一下如下這種狀況:
有些用戶不須要看到地圖。
既然用戶不須要看到地圖,咱們爲什麼還堅持不懈地加載它?這反而影響了咱們App的Performance。
此時,咱們就能夠利用ViewStub類了:
main.xml
- <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
- android:layout_width= "fill_parent"
- android:layout_height= "fill_parent" >
-
- <Button
- android:layout_width ="fill_parent"
- android:layout_height ="wrap_content"
- android:layout_gravity ="center_vertical"
- android:onClick ="onShowMap"
- android:text ="@string/show_map" />
-
- <ViewStub
- android:id ="@+id/map_stub"
- android:layout_width ="fill_parent"
- android:layout_height ="fill_parent"
- android:inflatedId ="@+id/map_view"
- android:layout ="@layout/map" />
- </RelativeLayout>
map.xml
- <com.google.android.maps.MapView xmlns:android ="http://schemas.android.com/apk/res/android"
- android:layout_width= "fill_parent"
- android:layout_height= "fill_parent"
- android:apiKey= "my_api_key"
- android:clickable= "true" />
接下來看看MainActivity
- public class MainActivity extends MapActivity {
-
- private View mViewStub;
-
- @Override
- public void onCreate (Bundle savedInstanceState ) {
- super. onCreate( savedInstanceState );
- setContentView( R. layout. main);
- mViewStub = findViewById( R. id. map_stub);
- }
-
- public void onShowMap (View v) {
- mViewStub. setVisibility (View .VISIBLE );
- }
- ....
- }
如你所見,在須要顯示圖像時咱們才調用onShowMap來改變map_stub的可見性。在改變以前,map_stub都不會渲染加載視圖資源。
小結:
1.當咱們的頁面變得複雜,XML文件內容過多時,<include />標籤能夠有效地幫助咱們整理文件內容,同時提升了XML文件的可讀性。同時,它的用法也與Fragment相似。
2.ViewStub是一個極佳的延遲加載視圖資源的方式。只要你設計的視圖是依賴於上下文來改變其可見性的,就利用ViewStub類吧。也許當你只將其應用在一個簡單的頁面當中時,並不會感受到在性能上有任何提高,可是在複雜頁面中,它的效果是極佳的。