使用数据绑定设置ImageView的色彩 [英] Set the tint of an ImageView using databinding
问题描述
我使用数据绑定来设置我的 ImageView
的色彩.这运作良好:
I use databinding to set the tint of my ImageView
. And this is working well :
android:tint="@{plantEntity.isFavorite ? @color/favorite : @color/favorite_none}" />
问题是 android:tint
已过时.当我尝试使用 app:tint
代替时,出现此错误:
The problem is android:tint
is deprecated. When I try to use app:tint
instead, I have this error :
Cannot find a setter for <android.widget.ImageView app:color> that accepts parameter type 'int'
If a binding adapter provides the setter, check that the adapter is annotated correctly and that the parameter type matches.
为什么以及我该怎么办?创建 BindingAdapter
吗?
Why and what I have to do ? Create a BindingAdapter
?
推荐答案
添加绑定适配器:
@BindingAdapter("app:tint")
fun ImageView.setImageTint(@ColorInt color: Int) {
setColorFilter(color)
}
,如果您的minSdk没有必要,则不必使用任何兼容性是>21(无论如何在2021年都是一笔不错的交易,您不应该支持低于26的任何东西)
and you don't have to use any compat if your minSdk is > 21 (which is a good deal anyway in 2021 you should not support anything below 26)
无论如何..这似乎是对 androidx.databinding表达式的迷惑 https://issuetracker.google.com/issues/152953070
anyhow .. this seems to be a fug on the androidx.databinding expression https://issuetracker.google.com/issues/152953070
这篇关于使用数据绑定设置ImageView的色彩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!