参考文档: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 ); } .... }