我正在使用新的TextInputLayout来包装EditText.当我确定某个字段有错误时,我会执行以下操作:
Drawable drawable = DrawableCompat.wrap(getEditText().getBackground());
DrawableCompat.setTintList(drawable, ColorStateList.valueOf(Color.RED));
这适用于5.0并将下划线变为红色,但在4.4或4.1测试设备上不执行任何操作.我在这里错过了什么?看起来如此简单,根据谷歌"正常工作"...非常确定我也有最新版本:
编译'com.android.support:design:22.2.0'
FWIW,如果我使用setColorFilter而不是setTint,那么它可以在所有平台上运行,但是我有问题就会消失,一旦焦点设置/左/等等就不会回来......我更愿意这样做色调(如果有人在寻找额外的信用lol,我真的更喜欢将色调应用于焦点和非焦点状态)
谢谢!
我正在使用DrawableCompat来调整drawable,如下所示,着色似乎不适用于API 19.我正在使用支持lib版本23.3.0
Drawable drawable = textView.getCompoundDrawables()[drawablePosition];
if (drawable != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            drawable.setTint(color);
        } else {
            DrawableCompat.setTint(DrawableCompat.wrap(drawable), color);
        }
    }
我已将ratingBar颜色设置为与Android默认蓝色和灰色不同的颜色 - 我已经将我的星星设为黑色背景,如果选中它们则为粉红色.
这是我的主要活动代码:
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingbar);
        LayerDrawable layerDrawable2 = (LayerDrawable) ratingBar.getProgressDrawable();
        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(0)),
                ContextCompat.getColor(getApplicationContext(), android.R.color.background_dark));
        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(1)),
                ContextCompat.getColor(getApplicationContext(), R.color.colorAccent)); // Partial star
        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(2)),
                ContextCompat.getColor(getApplicationContext(), R.color.colorAccent));
        ratingBar.setIsIndicator(false);
        ratingBar.setRating(3.6f);
        ratingBar.setStepSize(0.1f);
        ratingBar.invalidate();
        ratingBar.setIsIndicator(true);
    }
}
您可以看到,如果我将评级设为3.6,则最多可选择4.
但是,如果我注释掉我自己的自定义颜色,则ratingBar会正确设置为仅显示填充的第4个星的一部分,因为传递给它的值为3.6:
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingbar);
/*        LayerDrawable layerDrawable2 = (LayerDrawable) ratingBar.getProgressDrawable();
        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(0)),
                ContextCompat.getColor(getApplicationContext(), android.R.color.background_dark));
        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(1)),
                ContextCompat.getColor(getApplicationContext(), R.color.colorAccent)); …