此答案是基于@kris larson的建议。
我使用下面的方法,它在所有设备上工作正常。
setTintedCompoundDrawable这是一种自定义方法,它将TextView设置为您要设置复合可绘制,可绘制res ID &和您的颜色选择的res ID。
private void setTintedCompoundDrawable(TextView textView, int drawableRes, int tintRes) {
textView.setCompoundDrawablesWithIntrinsicBounds(
null, // Left
Utils.tintDrawable(ContextCompat.getDrawable(getContext(), drawableRes),
ContextCompat.getColor(getContext(), tintRes)), // Top
null, // Right
null); //Bottom
// if you need any space between the icon and text.
textView.setCompoundDrawablePadding(12);
}
色调tintDrawable方法是这样的:
public static Drawable tintDrawable(Drawable drawable, int tint) {
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, tint);
DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_ATOP);
return drawable;
}