我有自定义的 EditText
,我注意到他们离开 parent 时背景画错了。所以我在他们的 parent 身上使用了 android:clipChildren="false"
。那很好用。现在,当它们部分脱离父级时,它们可以正确绘制。
但这给了我一个新问题。在旧设备上(< Android 2.3?未确认此问题的最大版本是什么),背景不会被裁剪到它的填充。 EditText
背景现在绘制到屏幕的整个高度/宽度。这只发生在初始布局上。
有没有人遇到过这种情况?这真的很奇怪。我不明白为什么只有在使用 android:clipChildren="false"
并且仅在某些设备上背景才会绘制错误。我需要它,因为我的 EditText
可以被拖来拖去,并且需要在它们的父容器之外继续绘制。
最佳答案
我刚遇到同样的问题。这是由于将 ColorDrawables 作为背景(准确地说是一个包含多个 <selector>
项的 StateListDrawable ( @color/...
))造成的。
看起来这已在 Android 3.2.4-r1 中修复(提交 95930e1)。
在那之前,类(class)评论曾经说过:
Note that a ColorDrawable [...] ignores the Bounds, meaning it will draw everywhere in the current clip even if setBounds(...) was called with a smaller area.
这已被删除,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>
关于Android:clipChildren 导致自定义 View 的背景绘制全屏高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18061605/