【问题标题】:Can't get clipChildren=false attribute to work [duplicate]无法让 clipChildren=false 属性起作用 [重复]
【发布时间】:2013-04-05 05:43:25
【问题描述】:

我有一个黄色的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 中,我会得到以下信息:

【问题讨论】:

  • 相对布局有android:layout_height="44dp"。它本身被其父级剪辑
  • 我直接用 setContentView(R.layout.test); 设置了这个布局。没有明确的父母。根视图组不会填满整个屏幕吗?又为什么修改了LinearLayout的宽度?
  • @njzk2:为​​了进行更多测试,我尝试了不同的布局,但仍然无法正常工作(请参阅我的编辑)
  • 你必须把 android:clipChildren="false" 放在父 LinearLayout 中
  • 还是不行。请参阅我的第二次编辑。

标签: android view


【解决方案1】:

android:clipChildren="false" 允许每个孩子在其自己的界限之外,在父母之内绘图。它不允许孩子在父母本身之外进行绘画。为此,您需要在祖父母(以及)上设置 android:clipChildren="false"

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

【讨论】:

  • 非常感谢。关于“祖父母”的线索拯救了我的一天。
【解决方案2】:

也设置

android:clipToPadding="false"

旁边:

android:clipChildren="false"

【讨论】:

【解决方案3】:

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

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

在相对布局和繁荣!

说明

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 2017-03-04
    • 2015-04-28
    相关资源
    最近更新 更多