在布局优化中,Androi的官方提到了这三种布局<include />、<merge />、<ViewStub />,并介绍了这三种布局各有的优势,下面也是简单说一下他们的优势,以及怎么使用,记下来权当做笔记。
1、布局重用<include />
<include />标签能够重用布局文件,简单的使用如下:
<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)<include />标签可以使用单独的layout属性,这个也是必须使用的。
2)可以使用其他属性。<include />标签若指定了ID属性,而你的layout也定义了ID,则你的layout的ID会被覆盖,解决方案。
3)在include标签中所有的android:layout_*都是有效的,前提是必须要写layout_width和layout_height两个属性。
4)布局中可以包含两个相同的include标签,引用时可以使用如下方法解决(参考):
View bookmarks_container_2 = findViewById(R.id.bookmarks_favourite);
bookmarks_container_2.findViewById(R.id.bookmarks_list);
2、减少视图层级<merge />
<merge/>标签在UI的结构优化中起着非常重要的作用,它可以删减多余的层级,优化UI。<merge/>多用于替换FrameLayout或者当一个布局包含另一个时,<merge/>标签消除视图层次结构中多余的视图组。例如你的主布局文件是垂直布局,引入了一个垂直布局的include,这是如果include布局使用的LinearLayout就没意义了,使用的话反而减慢你的UI表现。这时可以使用<merge/>标签优化。
<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。更多<merge />介绍可以参考《Android Layout Tricks #3: Optimize by merging》
3、需要时使用<ViewStub />
<ViewStub />标签最大的优点是当你需要时才会加载,使用他并不会影响UI初始化时的性能。各种不常用的布局想进度条、显示错误消息等可以使用<ViewStub />标签,以减少内存使用量,加快渲染速度。<ViewStub />是一个不可见的,大小为0的View。<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();
当调用inflate()函数的时候,ViewStub被引用的资源替代,并且返回引用的view。这样程序可以直接得到引用的view而不用再次调用函数findViewById()来查找了。
注:ViewStub目前有个缺陷就是还不支持<merge />标签。
更多<ViewStub />标签介绍可以参考《Android Layout Tricks #3: Optimize with stubs》
* iOS入门群:83702688
* android开发进阶群:241395671
*/
参考:
http://developer.android.com/training/improving-layouts/reusing-layouts.html
http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html
http://developer.android.com/training/improving-layouts/optimizing-layout.html#Lint
http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html
http://developer.android.com/training/improving-layouts/loading-ondemand.html
分享到:
相关推荐
主要介绍了 Android抽象布局——include、merge 、ViewStub详解,详细的介绍了三种布局各有的优势,有兴趣的同学可以参考一下。
Android 官方提供了三个用来优化布局的标签,分别是include、merge与ViewStub,下面这篇文章主要给大家介绍了关于Android布局技巧之include、merge与ViewStub标签巧用的相关资料,需要的朋友可以参考下
android include merge标签
android中include和merge标记的区别和使用
Android中include和merge标签的基本使用方法
http://blog.csdn.net/i_do_can/article/details/51567913
merge结合include优化android布局,效果不知道,个人感觉使用上也有很大的局限,不过还是了解一下,记录下来。 布局文件都要有根节点,但android中的布局嵌套过多会造成性能问题,于是在使用include嵌套的时候我们...
为了高效复用完整布局,你可以使用<include>和<merge>标签嵌入另一个布局到当前布局。所以当你通过写一个自定义视图创建独立UI组件,你可以放到一个布局文件里,这样更容易复用。 复用布局因为其允许你创建可复用的...
Android 布局优化,(含 include、Viewstub、merge的用法)
Android UI优化之merge标签的使用,主要介绍ViewStub方法使用的注意事项及方法实现。
博客《ListView滑动删除实现之一——merge标签与LayoutInflater.inflate()》对应源码,博客地址:http://blog.csdn.net/harvic880925/article/details/45155965
NULL 博文链接:https://2528.iteye.com/blog/1138475
#include #include #include void init(int A[],int p,int r);//初始化数组 void print_A(int A[],int p,int r);//打印数组元素 void merge(int A[],int p,int q,int r);//合并排序算法 /************合并排序...
android merge 标签使用介绍
merge在include使用下优化布局文件
void merge_sort(int A[],int p,int r) { int q; if(p) { q=(p+r)/2;//计算q的值,即将问题拆分成两个子问题; merge_sort(A,p,q); //左半边递归调用merge_sort,缩小问题规模 printf("\n"); //print_A(A...
布局界面中include、merge、ViewStub的使用实例。