36

我有一个黄色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"

在此处输入图像描述

红色LinearLayout按预期剪裁

  • android:clipChildren="false"

在此处输入图像描述

其中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 中,我会得到以下信息:

在此处输入图像描述

4

3 回答 3

91

android:clipChildren="false" allows each child to draw outside of its own bounds, within the parent. It doesn't allow children to draw outside of the parent itself. For that you would need to set android:clipChildren="false" on the grandparent (as well).

I think what you're seeing with the colors is just because colors have no inherent bounds. If there is nothing clipping them, colors go forever. My theory is that if you used, say, a stretched 1x1 px image instead of a color, things would be different.

于 2014-10-23T13:08:08.147 回答
28

Also set

android:clipToPadding="false"

Beside:

android:clipChildren="false"
于 2016-10-24T08:01:04.937 回答
0

A trick which can solve your problem is to add some placeholder view

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

in the relative layout and boom!

Explanation

For me the boundary of relative layout seems to be different than the height. The boundary seems to be where the content ends or where the parent ends. Adding some view to the bottom sets the boudary at the bottom so everything works fine.

于 2018-07-01T09:34:33.803 回答