使用数据绑定设置ImageView的色彩 [英] Set the tint of an ImageView using databinding

查看:145
本文介绍了使用数据绑定设置ImageView的色彩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用数据绑定来设置我的 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 ?

推荐答案

类似于着色不起作用< 21版本在DataBinding中

添加绑定适配器:

@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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆