Android Drawable 在代码中实现android:tint效果

第一种不去区分版本,使用V4包的android.support.v4.graphics.drawable.DrawableCompat

ImageView image = new ImageView(context);
Drawable up = ContextCompat.getDrawable(context,R.drawable.ic_sort_up);
Drawable drawableUp= DrawableCompat.wrap(up);
DrawableCompat.setTint(drawableUp, ContextCompat.getColor(context,R.color.theme));
image.setImageDrawable(drawableUp);

第二种只能在API21以上使用

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
         
    ImageView image = new ImageView(context);
    image.setImageResource(R.drawable.ic_sort_down);
    image.setImageTintList(ColorStateList.valueOf(ContextCompat.getColor(context,R.color.theme)));
}

你可能感兴趣的:(Android笔记)