0
点赞
收藏
分享

微信扫一扫

Android 性能优化之布局优化


 在布局优化中,Androi的官方提到了这三种布局<include />、<merge />、<ViewStub />


1.重用布局include

    include标签常用于将布局中的公共部分提取出来供其他layout共用,以实现布局模块化,这在布局编写方便提供了大大的便利。例如,每个activity都会用到的titlebar.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background="@color/app_bg"
android:gravity="center_horizontal">

<include layout="@layout/titlebar"/>

<TextView android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:text="@string/hello"
android:padding="10dp" />



</LinearLayout>


注: 可以在include标签中覆盖被引用的根布局的

android:layout_*

属性,但是前提是android:layout_height和android:layout_width都要写。





2.减少布局层次merge

merge标签可用于两种典型情况:

1.布局顶结点是FrameLayout且不需要设置background或padding等属性,可以用merge代替,因为Activity内容视图的parent view就是个FrameLayout,所以可以用merge消除只剩一个。

2.某布局作为子布局被其他布局include时,使用merge当作该布局的顶节点,这样在被引入时顶结点会自动被忽略,而将其子节点全部合并到主布局中。


Android 性能优化之布局优化_xml


用hierarchy viewer查看main.xml布局如下图

<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>





Android 性能优化之布局优化_xml_02


注:listview的item布局根目录不能用merge,否则会报错:

merge can be used only with a valid ViewGroup root and attachToRoot=true

3.需要时加载ViewStub

    viewstub标签同include标签一样可以用来引入一个外部布局,不同的是,viewstub引入的布局默认不会扩张,即既不会占用显示也不会占用位置,从而在解析layout时节省cpu和内存。
    viewstub常用来引入那些默认不会显示,只在特殊情况下显示的布局,如进度布局、网络失败显示的刷新布局、信息出错出现的提示布局等。

<ViewStub
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" />


加载布局时,可以使用下面其中一种方法:

((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);
// or
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();


一旦visible/inflated,ViewStub元素将被layout属性值替换,并且android:inflatedId的值将作为根布局的id


注:
1. ViewStub目前有个缺陷就是还不支持 <merge /> 标签。
2. 要想获得stub_import中的控件,用如下代码


/必须使用inflate方法,否则下面获取不到子控件
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
importPanel.findViewById(R.id.children);




举报

相关推荐

0 条评论