參考文檔:http://blog.csdn.net/vector_yi/article/details/24402101
當activity中用到的布局較多較為復雜時,頁面渲染就會變得復雜,現匯總以下常用方法,提升頁面加載速度。
1、利用<include />標簽來避免重復渲染
當頁面中出現重復的布局時,如果只是復制粘貼,會顯得代碼陳余,並且繁瑣,使用<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的定義。
第二種方式,直接引用:
<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" />
二、使用<merge/>標簽
如果引入的是由多個控件組成的布局文件,為了避免多重繪制,可以使用<merge/>標簽
merge標簽用來消除在include一個布局到另一個布局時所產生的冗余view group。比如現在很多布局中會有兩個連續的Button,於是我們將這兩個連續的Button做成可復用布局(re-usable layout)。在使用include標簽時我們必須先將這兩個Button用一個view group比如LinearLayout組織在一起然后供其它布局使用,如果是include的地方也是LiearLayout就會造成有兩層連續的LiearLayout,除了降低UI性能沒有任何好處。這個時候我們就可以使用<merge/>標簽作為可復用布局的root view來避免這個問題。
<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>
當我們用<include/>標簽復用上述代碼時,系統會忽略merge元素,直接將兩個連續的Button放在<include/>標簽所在處。
<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 ); } .... }
