安卓 坑爹的 include 标签

在一个项目中我们可能会需要用到相同的布局设计,如果都写在一个xml文件中,代码显得很冗余,并且可读性也很差,所以我们可以把相同布局的代码单独写成一个模块,然后用到的时候可以通过<include /> 标签来重用layout代码。相当于html的include。


但是用这个标签忽然引用后的样式和被引用的样式居然不一样。。。见代码


usercenter_userinfo.xml 被引用的文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:background="@drawable/suma_selector_usercenter_item_round"
    android:orientation="horizontal"
    android:paddingLeft="10.0dip" >

.此处省略内部布局...

</RelativeLayout>




引用布局的文件:

<LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >


            <include
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dip"
                layout="@layout/usercenter_userinfo" />

        </LinearLayout>





明显看到,高度等样式居然变了。。

经过折腾,修改代码。。如下


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >


    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:background="@drawable/suma_selector_usercenter_item_round"
        android:orientation="horizontal"
        android:paddingLeft="10.0dip" >
. 此处省略内部布局...

        
    </RelativeLayout>


</LinearLayout>

样式终于正常了。。



总结:

安卓的include标签,有点让人疑惑,至于为什么子文件中的最外层布局为什么设置高度不生效。不太理解。反编译QQLive学习了下,他们也是嵌套了一层。

另外从文章 http://blog.csdn.net/liuzhidong123/article/details/7389745 可以看出,嵌套的xml文件可以不使用任何viewgroup布局。


如果你对include有高深的见解,请回复。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值