Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

android - DrawableCompat tinting does not work on pre-Lollipop

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));

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:

compile 'com.android.support:design:22.2.0'

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)

Thanks!

question from:https://stackoverflow.com/questions/30872101/drawablecompat-tinting-does-not-work-on-pre-lollipop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

Since version 23.2.0 of the support library you can also use setTint() instead of setTintList() to set just one tint color without having to create a ColorStateList.

DrawableCompat.setTint(wrappedDrawable, Color.RED);

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...