安卓 坑爹的 include 标签

来源:互联网 发布:朋友借身份证开淘宝店 编辑:程序博客网 时间:2024/06/05 08:41

在一个项目中我们可能会需要用到相同的布局设计,如果都写在一个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 0