Android 性能优化之布局优化
来源:互联网 发布:简述sql的特点 编辑:程序博客网 时间:2024/05/29 09:30
在布局优化中,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当作该布局的顶节点,这样在被引入时顶结点会自动被忽略,而将其子节点全部合并到主布局中。
用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>
注: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// or
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
注:
1. ViewStub目前有个缺陷就是还不支持 <merge /> 标签。
2. 要想获得stub_import中的控件,用如下代码
/必须使用inflate方法,否则下面获取不到子控件 View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate(); importPanel.findViewById(R.id.children);
0 0
- android 性能优化之布局
- Android性能优化之布局优化
- Android性能优化之布局优化
- Android性能优化之布局优化
- 性能优化之Android布局优化
- android性能优化之布局优化
- Android性能优化之布局优化
- Android性能优化之布局优化篇
- Android性能优化之布局优化
- Android性能优化之布局优化篇
- android性能优化之布局优化
- Android性能优化之布局优化总结
- Android性能优化之布局优化
- 10.1Android性能优化之布局优化
- android 性能优化之布局优化-----ViewStub
- Android性能优化之布局优化
- Android性能优化之布局优化
- Android性能优化之布局优化
- 关于Java协程和Kilim的一些概念
- SQL 语句优化
- spring-session实现分布式session共享及自定义sessionid
- Universal-Image-Loader,android-Volley,Picasso、Fresco和Glide五大Android开源组件加载网络图片的优缺点比较
- Hibernate 一对一连接表双向关联
- Android 性能优化之布局优化
- Java 开发 2.0: Kilim 简介
- c——隐式转换——复合类型
- android 格式化本地时间
- 指针降级
- 整理Rapid object detection using a boosted cascade of simple features论文中的要点
- Linux平台总线驱动设备模型
- 分布式搜索之搭建Solrcloud(Solr集群)
- SpringMVC学习笔记