Android抽象布局——include、merge 、ViewStub
2016-02-03 15:25
531 查看
最近学习了一些android布局优化方面的知识,android对布局优化提供了三种布局:
1、重用布局
1)标签可以使用单独的layout属性,这个也是必须使用的。
2)可以使用其他属性。标签若指定了ID属性,而你的layout也定义了ID,则你的layout的ID会被覆盖。
3)在include标签中所有的android:layout_*都是有效的,前提是必须要写layout_width和layout_height两个属性。
2、减少视图层级
这个标签在UI的结构优化中起着非常重要的作用,它可以删减多余的层级,优化UI。
现在,当你添加该布局文件时(使用标签),系统忽略节点并且直接添加两个Button。
3、需要时使用
这个标签最大的优点是当你需要时才会加载,使用他并不会影响UI初始化时的性能。各种不常用的布局想进度条、显示错误消息等可以使用这个标签,以减少内存使用量,加快渲染速度。
当你想加载布局时,可以使用下面其中一种方法:
当调用inflate()函数的时候,ViewStub被引用的资源替代,并且返回引用的view。 这样程序可以直接得到引用的view而不用再次调用函数findViewById()来查找了。
注:ViewStub目前有个缺陷就是还不支持 标签。
[code]<include/> <merge/> <ViewStub/>
1、重用布局
<include/>
[code]<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>
1)标签可以使用单独的layout属性,这个也是必须使用的。
2)可以使用其他属性。标签若指定了ID属性,而你的layout也定义了ID,则你的layout的ID会被覆盖。
3)在include标签中所有的android:layout_*都是有效的,前提是必须要写layout_width和layout_height两个属性。
2、减少视图层级
<merge/>
这个标签在UI的结构优化中起着非常重要的作用,它可以删减多余的层级,优化UI。
<merge/>多用于替换FrameLayout或者当一个布局包含另一个时,标签消除视图层次结构中多余的视图组。例如你的主布局文件是垂直布局,引入了一个垂直布局的include,这是如果include布局使用的LinearLayout就没意义了,使用的话反而减慢你的UI表现。这时可以使用标签优化。
[code]<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>
现在,当你添加该布局文件时(使用标签),系统忽略节点并且直接添加两个Button。
3、需要时使用
<ViewStub/>
这个标签最大的优点是当你需要时才会加载,使用他并不会影响UI初始化时的性能。各种不常用的布局想进度条、显示错误消息等可以使用这个标签,以减少内存使用量,加快渲染速度。
[code]<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" />
当你想加载布局时,可以使用下面其中一种方法:
[code]((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE); // or View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
当调用inflate()函数的时候,ViewStub被引用的资源替代,并且返回引用的view。 这样程序可以直接得到引用的view而不用再次调用函数findViewById()来查找了。
注:ViewStub目前有个缺陷就是还不支持 标签。
相关文章推荐
- Android初学习 - visibility属性VISIBLE、INVISIBLE、GONE
- LeakCanary——直白的展现Android中的内存泄露
- Android TextView HTML链接
- Android Edittext监听
- Android 6.0: 动态权限管理的解决方案
- 2016年1月 Android 开发技巧总汇
- Bugly Android 快速接入使用文档
- Android 使用动态载入框架DL进行插件化开发
- Android View事件机制 21问21答
- Android进阶推荐书籍
- android使用MPAndroidChart开源图表折线图
- Android RecycleView(一)
- Android studio 下的robotium自动化测试和持续集成
- Android studio 下的robotium自动化测试和持续集成
- 如何学习 Android Animation?
- Android 6.0 Mtp(二) 应用层代码分析
- Android启动流程
- Android 内存泄漏总结
- Android开发之查找本机的图片、视频、音乐、其他类别文件
- Android studio 下的robotium自动化测试和持续集成