使用数据绑定设置ImageView的色调

11

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

5个回答

15

在绑定适配器注释中不需要使用 app: 前缀。 - ubuntudroid

9

使用的是 androidx.appcompat.widget.AppCompatImageView,它已经在发挥作用。

app:tint 不再过时了。


2
android:tint已被弃用。但是,如果您使用androidx.appcompat.widget.AppCompatImageView而不是ImageView,则不会被弃用。 - Javad B
Must use app:tint instead of android:tint - user924
恰恰相反 - 使用 android:tint - divonas

4

使用 androidx.appcompat.widget.AppCompatImageView 并搭配 android:tint


1

只是为了补充@Jéwôm的答案,给出一个清晰的例子,使用AppCompatImageViewandroid:tint

<androidx.appcompat.widget.AppCompatImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:tint="@{data.yourImageTintColorRes}"
    android:src="@drawable/yourDrawable"/>

0
添加变量
<variable name="color" type="Integer" />

并且...

android:background="@{color == 0 ? @color/black : color == 1 ? @color/red: @color/purple_200 }"

在代码中,设置值。
 binding.color = 0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接