Android ImageViewのTintColorを変更する
Xmlから変更
android:tint="@color/hoge"
コードから変更
ImageViewのTintColorをコードから変更したい場合バージョンごとに差異があるのでメモです。
API21以上向けの場合は以下のようにしておけば問題なしです。
/** * 画像のTintColorを設定 * * @param color カラー */ fun ImageView.setTintColor(@ColorInt color: Int) { ImageViewCompat.setImageTintList(this, ColorStateList.valueOf(color)) } /** * 画像のTintColorを設定 * * @param resId カラーリソースID */ fun ImageView.setTintColorRes(@ColorRes resId: Int) { val color = ContextCompat.getColor(context, resId) ImageViewCompat.setImageTintList(this, ColorStateList.valueOf(color)) }