无法获取clipChildren = false属性

时间:2013-04-12 15:50:25

标签: android view

我有一个黄色RelativeLayout,其中包含更高的红色LinearLayout

为了使整个LinearLayout可见,我设置android:clipChildren="false",但这不会按预期工作:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="44dp"
    android:background="#FFFF00"
    android:clipChildren="false" >

    <LinearLayout
        android:layout_width="50dp"
        android:layout_height="100dp"
        android:background="#FF0000"
        android:orientation="vertical" >

    </LinearLayout>

</RelativeLayout>
  • android:clipChildren="true"

enter image description here

红色LinearLayout按预期剪裁

  • android:clipChildren="false"

enter image description here

其中LinearLayout高度被剪裁,并且不考虑布局中设置的宽度。

怎么了?

修改

如果我将容器包装在两个尺寸与其父级匹配的LinearLayout中,我得到相同的结果(我检查了LinearLayout容器的容器填满整个屏幕)。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:background="#FFFF00"
        android:clipChildren="false" >

        <LinearLayout
            android:layout_width="50dp"
            android:layout_height="100dp"
            android:background="#FF0000"
            android:orientation="vertical" >
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>

编辑2

如果我将android:clipChildren="false"属性放在父LinearLayout中,我会得到以下内容:

enter image description here

3 个答案:

答案 0 :(得分:65)

android:clipChildren="false"允许每个孩子在父母内部绘制自己的边界之外。它不允许孩子在父母本身之外画画。为此,您需要在祖父母(也是)上设置android:clipChildren="false"

我认为你所看到的颜色只是因为颜色没有固有的界限。如果没有任何剪裁它们,颜色将永远消失。我的理论是,如果你使用拉伸的1x1像素图像而不是颜色,那么事情会有所不同。

答案 1 :(得分:23)

同时设置

android:clipToPadding="false"

除了:

android:clipChildren="false"

答案 2 :(得分:0)

可以解决您的问题的技巧是添加一些占位符视图

<View
  android:layout_width="wrap_content"
  android:layout_height="1dp"
  android:background="?android:attr/listDivider"
  android:layout_alignParentBottom="true"/>

在相对布局和繁荣时期!

说明

对我来说,相对布局的边界似乎与高度不同。 边界似乎是内容结束或父项结束的地方。在底部添加一些视图可将底部设置为boudary,以便一切正常。