使用数据绑定设置 ImageView 的色调
Set the tint of an ImageView using databinding
我使用数据绑定来设置 ImageView
的色调。这运作良好:
android:tint="@{plantEntity.isFavorite ? @color/favorite : @color/favorite_none}" />
问题是 android:tint
已弃用。当我尝试改用 app:tint
时,出现此错误:
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
?
正在使用 androidx.appcompat.widget.AppCompatImageView
。
并且 app:tint
不再被弃用。
类似于 Tint does not work <21 version in DataBinding
上讨论的内容
添加绑定适配器:
@JvmStatic
@BindingAdapter("app:tint")
fun ImageView.setImageTint(@ColorInt color: Int) {
setColorFilter(color)
}
如果您的 minSdk 大于 21,则您不必使用 any compat(无论如何,在 2021 年这很划算,您不应该支持低于 26 的任何东西)
无论如何 .. 这似乎是 androidx.databinding expression https://issuetracker.google.com/issues/152953070
上的一个错误
将 androidx.appcompat.widget.AppCompatImageView
与 android:tint
结合使用。
我使用数据绑定来设置 ImageView
的色调。这运作良好:
android:tint="@{plantEntity.isFavorite ? @color/favorite : @color/favorite_none}" />
问题是 android:tint
已弃用。当我尝试改用 app:tint
时,出现此错误:
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
?
正在使用 androidx.appcompat.widget.AppCompatImageView
。
并且 app:tint
不再被弃用。
类似于 Tint does not work <21 version in DataBinding
上讨论的内容添加绑定适配器:
@JvmStatic
@BindingAdapter("app:tint")
fun ImageView.setImageTint(@ColorInt color: Int) {
setColorFilter(color)
}
如果您的 minSdk 大于 21,则您不必使用 any compat(无论如何,在 2021 年这很划算,您不应该支持低于 26 的任何东西)
无论如何 .. 这似乎是 androidx.databinding expression https://issuetracker.google.com/issues/152953070
上的一个错误将 androidx.appcompat.widget.AppCompatImageView
与 android:tint
结合使用。