尊重原创,转载请注明出处:http://blog.csdn.net/a740169405/article/details/50473909

Android 官方提供了三个用来优化布局的标签,分别是include、merge与ViewStub,其中ViewStub是动态加载视图到内存,大家可以查阅:Android UI布局优化之ViewStub

一、include布局重用:

在Android的应用程序开发中,标题栏是必不可少的一个元素,大部分页面都要用到,而且布局都是一样的,这时候使用include标签就显得极其的方便。使用时通常需要注意以下几点。

  1. include标签的layout_*属性会替换掉被include视图的根节点的对应属性。
  2. include标签的id属性会替换掉被include视图的根节点id
  3. 一个布局文件中支持include多个视图,但是这样会导致获取被include视图内的控件时,
    解决方法请参考:www.coboltforge.com/2012/05/tech-stuff-layout/

下面例子中,titlebar_layout.xml为标题栏布局,而activity_main.xml为主界面布局,activity_setting.xml为设置页面布局,这这两个界面中都include了titlebar_layout.xml视图。
titlebar_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/preference_activity_title_root"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="2dip"android:background="@drawable/zns_activity_title_bg"><TextView
        android:id="@+id/preference_activity_title_text"android:layout_width="match_parent"android:layout_height="45dip"android:gravity="center"android:text="123"android:textColor="#ffffff"android:textSize="18sp" /><ImageView
        android:id="@+id/preference_activity_title_image"android:layout_width="30dip"android:layout_height="25dip"android:layout_gravity="center_vertical"android:scaleType="fitCenter"android:layout_marginLeft="5dip"android:src="@drawable/common_menu_selector_white" />
</FrameLayout>

主界面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:background="#000000"><include layout="@layout/titlebar_layout"></include><TextView
        android:layout_width="match_parent"android:layout_height="match_parent"android:text="这是内容区域"android:gravity="center"android:textSize="25sp"android:textColor="#ffffff"/>
</LinearLayout>

当然,其他界面使用include同样能包含该标题栏。

一、通过merge减少视图节点:

merge翻译成中文是合并的意思,在Android中通过使用merge能够减少视图的节点数,
从而减少视图在绘制过程消耗的时间,达到提高UI性能的效果。使用merge时通常需要注意以下几点:

  1. merge必须放在布局文件的根节点上。
  2. merge并不是一个ViewGroup,也不是一个View,它相当于声明了一些视图,等待被添加。
  3. merge标签被添加到A容器下,那么merge下的所有视图将被添加到A容器下。
  4. 因为merge标签并不是View,所以在通过LayoutInflate.inflate方法渲染的时候, 第二个参数必须指定一个父容器,且第三个参数必须为true,也就是必须为merge下的视图指定一个父亲节点。
  5. 如果Activity的布局文件根节点是FrameLayout,可以替换为merge标签,这样,执行setContentView之后,会减少一层FrameLayout节点。
  6. 自定义View如果继承LinearLayout,建议让自定义View的布局文件根节点设置成merge,这样能少一层结点。
  7. 因为merge不是View,所以对merge标签设置的所有属性都是无效的。

其中第一点,我们看看LayoutInflate类的源码说明:

} else if (TAG_MERGE.equals(name)) {// 如果merge不是根节点,报错throw new InflateException("<merge /> must be the root element");
}

其中第三点,常用在自定义View中遇到,附上系统LayoutInflate类,对于该现象的源码:

if (TAG_MERGE.equals(name)) {// 如果是merge标签,指定的root为空,或则attachToRoot为false,则抛出异常信息if (root == null || !attachToRoot) {throw new InflateException("<merge /> can be used only with a valid "+ "ViewGroup root and attachToRoot=true");}rInflate(parser, root, attrs, false, false);
}

针对第五点,做一下对比:
布局文件1:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"><Button
        android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="top"android:text="顶部Button" /><Button
        android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="bottom"android:text="底部Button" /></merge>

效果1:

布局文件2:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><Button
        android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="top"android:text="顶部Button" /><Button
        android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="bottom"android:text="底部Button" /></FrameLayout>

效果2:

我们可以看到,如果使用merge,明显少了一个FrameLayout节点,这也算一个视图优化技巧。

下面对第六条(自定义View如果继承LinearLayout,建议让自定义View的布局文件根节点设置成merge,这样能少一层结点)进行分析:
先看看效果,就是一个线性布局,上下各一个TextView,看看使用merge和不使用merge的视图节点,
以及使用merge的时候layoutInflate类的注意点。
效果图:

第一种情况(不使用merge):
布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextView
        android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1.0"android:background="#000000"android:gravity="center"android:text="第一个TextView"android:textColor="#ffffff" /><TextView
        android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1.0"android:background="#ffffff"android:gravity="center"android:text="第一个TextView"android:textColor="#000000" /></LinearLayout>

代码:

/*** 自定义的View,竖直方向的LinearLayout*/
public class MergeLayout extends LinearLayout {public MergeLayout(Context context) {super(context);LayoutInflater.from(context).inflate(R.layout.merge_activity, this, true);}
}

视图树:

我们发现,MergeLayout这个自定义控件的下面并不是直接跟着两个TextView,
而是多了一个LinearLayout。

第二种情况(使用merge):
注意因为为merge标签的设置的属性都不会生效,所以原来LinearLayout标签上的属性需要转移到java代码中设置。
布局文件:

<?xml version="1.0" encoding="utf-8"?>
<!-- 习惯性的标记一下,MergeLayout布局 android:orientation="vertical" -->
<merge xmlns:android="http://schemas.android.com/apk/res/android" ><TextView
        android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1.0"android:background="#000000"android:gravity="center"android:text="第一个TextView"android:textColor="#ffffff" /><TextView
        android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1.0"android:background="#ffffff"android:gravity="center"android:text="第一个TextView"android:textColor="#000000" /></merge>

个人习惯在用merge的时候在旁边标明使用到的属性,以防忘记。

java代码中需要设置orientation属性:

/*** 自定义的View,竖直方向的LinearLayout*/
public class MergeLayout extends LinearLayout {public MergeLayout(Context context) {super(context);// 设置为数值方向的布局setOrientation(VERTICAL);LayoutInflater.from(context).inflate(R.layout.merge_activity, this, true);}
}

再看看视图树:

我们发现,LinearLayout节点被去掉了。但是最终显示给用户的界面却是一样的。

总结

1. 使用include标签可以增加布局的复用性,提高效率。
2. 使用merge标签可以减少视图树中的节点个数,加快视图的绘制,提高UI性能。
3. merge标签的使用,看上去一次只减少一个节点,但是当一个布局嵌套很复杂的时候,
节点的个数可能达到几百个,这个时候,如果每个地方都多一个节点,视图的绘制时间相应的也就变长了很多。

UI性能的优化还有另外一个比较重要的知识点ViewStub,它是一个View,但是它几乎不占用资源,
使用ViewStub能够加快视图的绘制,提高性能,关于ViewStub的知识,大家可以参看博文:
Android UI布局优化之ViewStub

Android 布局优化之include与merge相关推荐

  1. android layout include merge,Android 布局优化之include与merge

    Android 官方提供了三个用来优化布局的标签,分别是include.merge与ViewStub,其中ViewStub是动态加载视图到内存,大家可以查阅:Android UI布局优化之ViewSt ...

  2. Android布局优化之include、merge、ViewStub

    include:引入重复使用的相同布局 merge:减少include布局的层级,将子元素直接添加到merge标签的parent中 ViewStub:其实就是一个宽高都为0的一个View,它默认是不可 ...

  3. 在android布局中使用include和merge标签

    在我们开发Android布局时,经常会有很多的布局是相同的,这个时候我们可以通过<include/>和<merge/>标签实现将复杂的布局包含在需要的布局中,减少重复代码的编写 ...

  4. Android布局加载慢,Android布局优化(四)X2C — 提升布局加载速度200%

    系列文章 前言 在Android布局优化(一)从布局加载原理说起中我们说到了布局加载的两大性能瓶颈,通过IO操作将XML加载到内存中并进行解析和通过反射创建View.这里介绍一种避免运行时通过IO操作 ...

  5. 一篇文章搞定《Android布局优化》

    ------<一篇文章搞定Android布局优化> 前言 为什么要进行布局优化? Android绘制原理 双缓冲机制 布局加载原理 布局加载优化的一些方法介绍 AsyncLayoutInf ...

  6. 深入探索Android布局优化(上)

    前言 成为一名优秀的Android开发,需要一份完备的知识体系,在这里,让我们一起成长为自己所想的那样~. Android的绘制优化其实可以分为两个部分,即布局(UI)优化和卡顿优化,而布局优化的核心 ...

  7. android listview viewstub,Android布局优化之ViewStub控件

    ViewStub是Android布局优化中一个很不错的标签/控件,直接继承自View.虽然Android开发人员基本上都听说过,但是真正用的可能不多. ViewStub可以理解成一个非常轻量级的Vie ...

  8. Android布局文件之 include 详细介绍

    Android布局文件之 include 详细介绍 include简介 众所周知,include就是在一个布局中,导入另一个布局文件. 优势是:相同的页面只需写一次,提高了共通布局的复用性. 下面我们 ...

  9. Android布局优化之ViewStub、include、merge使用与源码分析

    在开发中UI布局是我们都会遇到的问题,随着UI越来越多,布局的重复性.复杂度也会随之增长.Android官方给了几个优化的方法,但是网络上的资料基本上都是对官方资料的翻译,这些资料都特别的简单,经常会 ...

  10. android 减少布局层级,Android 布局优化

    布局优化主要从以下几点进行着手 减少布局层次 和 复杂度 优化绘制流程 按需加载布局 减少布局层次 和 复杂度 首先我们可以通过以下工具分析界面布局的结构 查看布局树工具:Hierarchy View ...

最新文章

  1. python多线程的几种方法
  2. 使用ISDN和DDR技术改善远程连接性能
  3. Maltego更新到4.2.4.12374
  4. 深入Java虚拟机——类型装载、连接(转)
  5. 在VS中巧用文件系统来发布网站
  6. word2vec 构建中文词向量
  7. Hive动态分区 参数配置及语法
  8. 初中学历在辽宁学计算机,在辽宁省实验中学学习是怎样一番体验?
  9. mysql注解实体类_jpa实体类生成mysql表及字段注解
  10. (二)Flask 学习 —— 模板
  11. 2012年软件开发者薪资调查报告
  12. 测控技术与仪器是计算机相关的,测控技术与仪器专业
  13. Mac 卸载Symantec软件
  14. 私有云部署和本地化部署有什么区别?
  15. 松翰SN8P2511 SOP8单片机 可代烧录 提供单片机方案开发 单片机解密
  16. 图解Linux网络包接收过程
  17. Prisma(三)——数据模型
  18. 资料共享库 | ApacheCN(apache中文网)
  19. 飞上天和太阳肩并肩中国人这项太阳能开发技术牛大发了
  20. “Elasticsearch + Kibana + ik分词器“介绍与使用

热门文章

  1. settings.xml‘ has syntax errors
  2. 快速突破面试算法之链表篇
  3. 无法访问 请与这台计算机,我们办公室两台电脑想连接一个共享的打印机,但是连接的时候,总是显示无法访问,请与这台计算机的管理员联系,应该怎么设置啊...
  4. 在什么场合里你会用到消息队列?
  5. spring相关技术实现的核心原理
  6. scrollView滚动原理
  7. 快速开发jQuery插件的10大技巧(转)
  8. VC++ 6.0 快捷键
  9. 有抱负的程序员应看的10个TED演讲
  10. 动易BizIdea后台添加自定义功能