How to use setImageTintList() on Android API <

2020-06-01 08:55发布

imgView.setImageTintList(getResources()
      .getColorStateList(R.color.my_clr_selector));

It says 'Call requires API level 21'.

How can I make it work on Android devices below API 21?

I can make it work by using ImageView#setColorFilter() but I prefer to use a ColorStateList to set tint.

3条回答
▲ chillily
2楼-- · 2020-06-01 09:18
ImageViewCompat.setImageTintList(ivImage, ColorStateList.valueOf(ContextCompat.getColor(context, R.color.primaryColor)));
查看更多
Viruses.
3楼-- · 2020-06-01 09:21

You should use ImageViewCompat#setImageTintList() to achieve this. On API 21+, it will use ImageView#setImageTintList() as you would expect... and on older platform versions, it will delegate to AppCompatImageView which provides a backported implementation.

ColorStateList csl = AppCompatResources.getColorStateList(context, R.color.my_color_state_list);
ImageViewCompat.setImageTintList(imageView, csl);
查看更多
我命由我不由天
4楼-- · 2020-06-01 09:21

This is now available in Support Library 25.4.0. See Link

ImageViewCompat.setImageTintList(imageView, colorStateList)

查看更多
登录 后发表回答