我在样式中的默认色调强调颜色为蓝色,并且在发生错误时,通过编程方式将其更改为红色,如以下代码所示

    Drawable wrappedDrawable = DrawableCompat.wrap(mUsername.getBackground());
    DrawableCompat.setTint(wrappedDrawable, ContextCompat.getColor(getActivity(), R.color.red_error));


但是,当我重新启动应用程序时,颜色是红色,如何将其设置回style.xml中的默认颜色?

最佳答案

在您的情况下,最好使用colorFilter而不是tintcolor:

//get reference on drawable
Drawable wrappedDrawable = DrawableCompat.wrap(mUsername.getBackground());
//get color ressource with Android M SDK
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    wrappedDrawable.setColorFilter(this.getColor(R.color.blue), PorterDuff.Mode.MULTIPLY);
else
    //classic method
    wrappedDrawable.setColorFilter(this.getResources().getColor(R.color.blue), PorterDuff.Mode.MULTIPLY);


要删除滤镜颜色,只需使用clearColorFilter:

wrappedDrawable.clearColorFilter();

10-06 10:05