【Android】Merge讲解与实例

来源:互联网 发布:简单的java web项目 编辑:程序博客网 时间:2024/05/02 08:21

单独将<merge/>标签做个介绍,是因为它在优化UI结构时起到很重要的作用。目的是通过删减多余或者额外的层级,从而优化整个Android Layout的结构。核心功能就是减少冗余的层次从而达到优化UI的目的!

将通过一个例子来了解这个标签实际所产生的作用,这样可以更直观的了解<merge/>的用法。

建立一个简单的Layout,其中包含两个Views元素:ImageView和TextView 默认状态下我们将这两个元素放在FrameLayout中。其效果是在主视图中全屏显示一张图片,之后将标题显示在图片上,并位于视图的下方。以下是xml代码:

<?xmlversion="1.0"encoding="utf-8"?>

<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"

   android:layout_width="fill_parent"

   android:layout_height="fill_parent">

 

   <ImageView

       android:layout_width="fill_parent"

       android:layout_height="fill_parent"

       android:scaleType="center"

       android:src="@drawable/rain"/>

 

   <TextView

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_marginBottom="20dip"

       android:layout_gravity="center_horizontal|bottom"

       android:padding="12dip"

       android:background="#AA000000"

       android:textColor="#ffffffff"

       android:text="GoldenGate2"/>

      

</FrameLayout>

应用上边的Layout运行的视图为

【Android】Merge讲解与实例

启动 tools>hierarchyviewer.bat工具查看当前UI结构视图

有关tools>hierarchyviewer.bat的知识见

http://jingyan.baidu.com/album/8ebacdf0cc614849f65cd5f4.html?picindex=2

【Android】Merge讲解与实例

我们可以很明显的看到出现了两个framelayout节点,很明显这两个完全意义相同的节点造成了资源浪费(这里可以提醒大家在开发工程中可以习惯性的通过hierarchyViewer查看当前UI资源的分配情况),那么如何才能解决这种问题呢(就当前例子是如何去掉多余的frameLayout节点)?这时候就要用到<merge/>标签来处理类似的问题了。我们将上边xml代码中的framLayout替换成merge:

<?xmlversion="1.0"encoding="utf-8"?>

<mergexmlns:android="http://schemas.android.com/apk/res/android">

 

   <ImageView

       android:layout_width="fill_parent"

       android:layout_height="fill_parent"

       android:scaleType="center"

       android:src="@drawable/rain"/>

 

   <TextView

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_marginBottom="20dip"

       android:layout_gravity="center_horizontal|bottom"

       android:padding="12dip"

       android:background="#AA000000"

       android:textColor="#ffffffff"

       android:text="GoldenGate2"/>

      

</merge>

UI结构图:

【Android】Merge讲解与实例

运行程序后在Emulator中显示的效果是一样的,可是通过hierarchyviewer查看的UI结构是有变化的,当初多余的FrameLayout节点被合并在一起了,或者可以理解为将merge标签中的子集直接加到Activity的FrameLayout跟节点下(这里需要提醒大家注意:所有的Activity视图的根节点都是frameLayout)。如果你所创建的Layout并不是用framLayout作为根节点(而是应用LinerLayout等定义root标签),就不能应用上边的例子通过merge来优化UI结构。

除了上边的例子外,meger还有另外一个用法,当应用Include或者ViewStub标签从外部导入xml结构时,可以将被导入的xml用merge作为根节点表示,这样当被嵌入父级结构中后可以很好的将它所包含的子集融合到父级结构中,而不会出现冗余的节点。

 

另外有两点需要特别注意

<merge/>只可以作为xml layout的根节点。

当需要扩充的xml layout本身是由merge作为根节点的话,需要将被导入的xml layout置于 viewGroup中,同时需要设置attachToRoot为True。


关于LayoutInflater类inflate(int resource, ViewGroup root, boolean attachToRoot)方法三个参数的含义

resource:需要加载布局文件的id,意思是需要将这个布局文件中加载到Activity中来操作。

root:需要附加到resource资源文件的根控件,什么意思呢,就是inflate()会返回一个View对象,如果第三个参数attachToRoot为true,就将这个root作为根对象返回,否则仅仅将这个root对象的LayoutParams属性附加到resource对象的根布局对象上,也就是布局文件resource的最外层的View上,比如是一个LinearLayout或者其它的Layout对象。

attachToRoot:是否将root附加到布局文件的根视图上


0 0
原创粉丝点击