3

我有风俗习惯EditText,当他们离开父母时,我注意到他们的背景画错了。所以我用android:clipChildren="false"在他们的父母身上。这很好用。他们现在在部分脱离父母时正确绘制。

不过,这给了我一个新问题。在较旧的设备上(< Android 2.3?未确认此问题的最高版本是多少),背景不会被裁剪到它的填充。背景现在EditText正在绘制到屏幕的整个高度/宽度。这只发生在初始布局上。

有谁之前经历过这个吗?这真的很奇怪。我不明白为什么只有在使用时android:clipChildren="false"并且仅在某些设备上才会出现背景错误。不过我需要它,因为我EditText的 s 可以被拖动并且需要在其父容器之外继续绘制。

4

2 回答 2

1

我刚刚遇到了同样的问题。这是由于将 ColorDrawables 作为背景(确切地说,是一个<selector>包含多个@color/...项目的 StateListDrawable ())引起的。

看起来这已在 Android 3.2.4-r1 中修复(提交95930e1)。

在此之前,班级评论曾经说过:

请注意,ColorDrawable [...] 会忽略 Bounds,这意味着即使 setBounds(...) 调用的区域较小,它也会在当前剪辑中的任何地方绘制。

这已被删除,并且draw(Canvas)方法更改为尊重边界。


作为一种解决方法,如果您需要支持较旧的 Android 版本,您可以使用具有纯色的 ShapeDrawable 来获得相同的行为:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
       android:shape="rectangle">
    <solid android:color="@color/your_background_color"/>    
</shape>
于 2014-03-31T16:14:03.793 回答
0

我们有一个 2.3.4 设备,这个问题导致 ImageView 覆盖它所包含的 LinearLayout 中的所有内容。修复是创建上面提到的 ShapeDrawable 并将其用作图像的背景。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
       android:shape="rectangle">
    <solid android:color="@color/your_background_color"/>    
</shape>

<ImageView
    android:id="@+id/this_is"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_gravity="bottom"
    android:background="@drawable/a_rectangle_shape_drawable"
    android:contentDescription="@string/an_image_view"
    android:scaleType="centerInside"
    android:src="@drawable/an_icon" />
于 2014-07-14T20:05:51.230 回答