问题描述
imgView.setImageTintList(getResources()
.getColorStateList(R.color.my_clr_selector));
上面写着调用需要API级别21".
It says 'Call requires API level 21'.
如何使它在API 21以下的Android设备上运行?
How can I make it work on Android devices below API 21?
我可以使用ImageView#setColorFilter()
使其起作用,但我更喜欢使用ColorStateList
设置色调.
I can make it work by using ImageView#setColorFilter()
but I prefer to use a ColorStateList
to set tint.
推荐答案
您应该使用 ImageViewCompat#setImageTintList()
即可实现.在API 21+上,它将像您期望的那样使用ImageView#setImageTintList()
...,而在较旧的平台版本上,它将委派给AppCompatImageView
,后者提供了反向移植的实现.
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);
这篇关于如何在Android API<上使用setImageTintList() 21岁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!