layout xml中标签 include

作者: 糖葫芦_倩倩 | 来源:发表于2017-07-11 14:34 被阅读129次

在我们的实际开发中,我们的xml出于复用,常常使用到 <include layout= "" /> 这样的,引入外部的 layout ,但是引入之后,发现一个问题就是无法使用其它的属性。

例如下面的代码:

<include
        layout="@layout/share_layout"
        android:layout_alignParentBottom="true" />

然后你就会发现会报这样的错误:

img.png

大概意思是说:这个 layout_alignParentBottom 将会被忽略到 ,除非含有 layout_widthlayout_height 这两个属性也在<include>标签内。

之前没有注意这个问题,采取的解决办法如下:

解决方案1

就是在include标签外又嵌套了一层布局:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <include layout="@layout/share_layout" />

 </LinearLayout>

这个在一开始确实可以解决问题,但是我们的原则是尽量布局层次越少越好,能一个写完的绝对不写两个,于是第二种方案就诞生了(基于之前编译器给我报的错误)。

解决方案2

<include
        layout="@layout/share_layout"
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:layout_alignParentBottom="true" />

最后:问题解决,要善于观察错误,并从中找到解决方案。

相关文章

网友评论

    本文标题:layout xml中标签 include

    本文链接:https://www.haomeiwen.com/subject/nqqmhxtx.html