DrawableCompat着色不能在pre-棒棒糖工作 [英] DrawableCompat tinting does not work on pre-Lollipop
问题描述
我使用的是新TextInputLayout来包装一个EditText。当我确定一个字段我做了以下错误:
I'm using the new TextInputLayout to wrap an EditText. When I determine a field has an error I do the following:
Drawable drawable = DrawableCompat.wrap(getEditText().getBackground());
DrawableCompat.setTintList(drawable, ColorStateList.valueOf(Color.RED));
这适用于5.0和曲折的红色下划线,但会在4.4或4.1的测试设备什么都没有。我在想什么吗?看起来很简单,按照谷歌只是工作... pretty的肯定我有它的最新版本,以及:
This works on 5.0 and turns the underline red, but does nothing on 4.4 or 4.1 test devices. What am I missing here? Seems so simple and according to google "just works"... pretty sure I have the latest version of it as well:
编译com.android.support:design:22.2.0
compile 'com.android.support:design:22.2.0'
FWIW,如果我做setColorFilter,而不是setTint那么它适用于所有平台,但后来我有问题,它会离开,不要回来,尽快为重点设置/左/等...我倒是preFER与色调做到这一点(和真正的preFER有色调适用于重点和非重点国家,如果有人正在寻找额外的分数笑)
FWIW, if I do setColorFilter instead of setTint then it works on all platforms but then I have issues with it going away and not coming back as soon as the focus is set/left/etc... I'd prefer to do it with tint (and really prefer to have the tint apply to the focus and non-focus states if anybody is looking for extra credit lol)
谢谢!
推荐答案
当你调用包裹()
那么原来的绘制对象
被包裹内进入一个新的 DrawableWrapper
这是用来实现对旧设备的着色。因此,要使它工作,你必须设置返回绘制对象
回的EditText
:
When you call wrap()
then the original Drawable
is wrapped internally into a new DrawableWrapper
which is used to implement the tinting on older devices. So to make it work you have to set the returned Drawable
back to the EditText
:
final Drawable originalDrawable = editText.getBackground();
final Drawable wrappedDrawable = DrawableCompat.wrap(originalDrawable);
DrawableCompat.setTintList(wrappedDrawable, ColorStateList.valueOf(Color.RED));
editText.setBackground(wrappedDrawable);
如果你想确保向后超越API级别16的兼容性,运行了一个小障碍
。 的setBackground()
中添加了API级别16,你需要调用 setBackgroundDrawable()
之前的设备。这是最好的实现它会替你一个辅助方法:
If you want to ensure backwards compatibility beyond API level 16 you run into a little snag. setBackground()
was added in API level 16 and you need to call setBackgroundDrawable()
on devices before that. It's best to implement a helper method which does that for you:
public static void setBackground(View view, Drawable background) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(background);
} else {
view.setBackgroundDrawable(background);
}
}
这篇关于DrawableCompat着色不能在pre-棒棒糖工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!