android 布局渲染效率,android性能优化-渲染-嵌套标签

1.重用布局include

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

android:layout_width=”match_parent”

android:layout_height="wrap_content"

android:background="@color/titlebar_bg">

android:layout_height="wrap_content"

android:src="@drawable/gafricalogo" />

复制代码

用include引用:

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

...

复制代码

注: 可以在include标签中覆盖被引用的根布局的android:layout_*属性,但是前提是android:layout_height和android:layout_width都要写,如:

android:layout_width=”match_parent”

android:layout_height=”match_parent”

layout=”@layout/title”/>复制代码

2.减少布局层次merge

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

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

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

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

2ac06dd366e77979daa9b55a48bf83d8.png

1.png

如图,红色部分的RelativeLayout是多余的,解决如下: <?xml version="1.0" encoding="utf-8"?>

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/button"

android:layout_width="match_parent"

android:layout_height="@dimen/dp_40"

android:layout_above="@+id/text"/>

android:id="@+id/text"

android:layout_width="match_parent"

android:layout_height="@dimen/dp_40"

android:layout_alignParentBottom="true"

android:text="@string/app_name" />

复制代码

结果如下图:

91279d06d83ae0c8df48803d1f8d5f3e.png

2.png

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

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

3.需要时加载ViewStub

viewstub标签同include标签一样可以用来引入一个外部布局,不同的是,viewstub引入的布局默认不会扩张,即既不会占用显示也不会占用位置,从而在解析layout时节省cpu和内存。

viewstub常用来引入那些默认不会显示,只在特殊情况下显示的布局,如进度布局、网络失败显示的刷新布局、信息出错出现的提示布局等。

定义如下:

android:id="@+id/stub_import"

android:inflatedId="@+id/panel_import"

android:layout="@layout/stub_import"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_gravity="bottom" />复制代码

加载ViewStub: ((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目前有个缺陷就是还不支持 标签。

2. 要想获得stub_import中的控件,用如下代码 //必须使用inflate方法,否则下面获取不到子控件

View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();

importPanel.findViewById(R.id.children);复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值