Merge标签使用

今天看了下<merge />标签的使用,总结如下:
1.merge标签中的子集是直接加到Activity的FrameLayout根节点下,(Activity视图的根节点都是frameLayout).如果你所创建的Layout并不是用framLayout作为根节点(而是应用LinerLayout等定义root标签),就不能通过merge来优化UI结构.

2.当应用Include或者ViewStub标签从外部导入xml结构时,可以将被导入的xml用merge作为根节点表示,这样当被嵌入父级结构中后可以很好的将它所包含的子集融合到父级结构中,而不会出现冗余的节点.



<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns: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:background="@drawable/bg" />
<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="Golden Gate" />
</FrameLayout>


大家可以将根节点FrameLayout更改成merge标签进行测试,显示效果是一样,但是我们通过sdk中的tools> hierarchyviewer.bat进行视图查看,我们可以看到区别.

另外需要注意的是:
1.<merge />只可以作为xml layout的根节点.

2.当需要扩充的xml layout本身是由merge作为根节点的话,需要将被导入的xml layout置于 viewGroup中,同时需要设置attachToRoot为True.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值