android merge标签
我们在写android xml布局文件时,有时候会产生多余的view树节点,当android进行解析时,多余的view节点会导致解析时间加长,进而导致
页面跳转变慢,变的卡顿。怎样避免这种情况呢?我们可以通过hierarchyviewer去查看布局文件的view 节点树,根据这个节点树利用merge
标签优化掉多余的节点。下面我们就使用一个例子来掩饰怎样使用merge标签来优化布局文件。
首先我们来说一下merge的特点:
1、merge标签只能用于xml layout文件的根元素
2、如果将一个以merge标签为根元素的xml文件作为布局文件的话,那么需要使用inflate (int resource, ViewGroup root, boolean attachToRoot)
指定一个ViewGroup 作为其容器,并且要设置attachToRoot 为true。
好了我们以两个不同的布局文件来对比使用merge标签后,view树的变化。例子是这样的:我自定义了一个view,然后定义了这个view的布局文
件,然后在Activity的布局中引用这个view,我们就使用merge来优化这个view的布局文件。
view的布局xml代码
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:text="发送"/> <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/send" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_marginRight="20dp" android:ems="10"> <requestFocus android:focusable="true"/> </EditText></RelativeLayout>
activity的xml布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:auto="http://schemas.android.com/apk/res/com.lonuery.autowidget" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:textSize="25sp" android:text="地方"/> <com.lonuery.autowidget.AutoView android:id="@+id/auto" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@+id/tv" android:layout_marginTop="15dp" auto:attr1="发送" auto:attr2="自定义属性"> </com.lonuery.autowidget.AutoView></RelativeLayout>
然后我们使用hierarchyviewer来查看这个activity的xml布局节点
根据这个图片我们可以看到,中间有一个RelativiLayout无效的节点,它是AutoView的唯一子元素,既然是唯一子元素那么他和父元素就有相同的尺寸
并且也没有定义background,以及padding和gravity,那么完全可以直接将这个节点去掉。
使用merge优化AutoView布局文件,使用merge标签替换RelativeLayout
<?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:text="发送"/> <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/send" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_marginRight="20dp" android:ems="10"> <requestFocus android:focusable="true"/> </EditText></merge>
然后我们再来看activity的布局节点图
优化布局的目的。
通过以上例子我们了解到了merge标签的使用,那么merge标签在什么时候可以去使用呢?
当一个xml布局文件在另一个布局下被引用时,如果这两个布局相同,并且这个xml文件的大小尺寸和其父布局文件相同,也就是这个布局文件没有设置
background,gravity以及padding等属性时,就能够使用merge标签进行优化。简单的说就是:当一个xml布局文件A,然后在另一个xmlB中被引用,假
设A被放置在RelativeLayout中,那么只要A的根布局是RelativeLayout布局,并且符合上述条件,那么布局文件A的根布局RelativeLayout就能够使用merge
标签进行优化。如上述例子就是在Activity引入了自定义控件AutoView,那么就要在activity的布局文件中引入AutoView的布局,由于AutoView的布局的父
布局是RelativeLayout,那么在AutoView布局文件中就能够使用merge进行优化。
参考博客:http://www.cnblogs.com/xirihanlin/archive/2010/04/23/1718353.html
更多相关文章
- android Apk打包过程概述_android是如何打包apk的
- 如何辨别你的android G1手机是否刷新SPL?
- Android流式布局FlowLayout的实现,Android布局的内部机制onMeasur
- Android学习笔记_23_服务Service之AIDL和远程服务实现进程通信以
- Android开发之文件操作详解
- 04 ImageView中图片保存到文件
- (三) 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱
- Android(安卓)studio 下出现"Cannot resolve symbol R"的解决方
- 一种巧妙的绕过Android锁屏密码的方法