【问题标题】:Set the tint of an ImageView using databinding使用数据绑定设置 ImageView 的色调
【发布时间】:2022-03-23 19:35:33
【问题描述】:

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

【问题讨论】:

    标签: android android-databinding


    【解决方案1】:

    它正在工作并使用androidx.appcompat.widget.AppCompatImageView

    并且app:tint 不再被弃用。

    【讨论】:

    • android:tint 已弃用。但如果您使用 androidx.appcompat.widget.AppCompatImageView 而不是 ImageView 则不推荐使用
    • Must use app:tint instead of android:tint
    • 恰恰相反 - 使用android:tint
    【解决方案2】:

    类似于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 上的一个问题

    【讨论】:

    • Binding Adapter 注解中不需要app: 前缀。
    【解决方案3】:

    androidx.appcompat.widget.AppCompatImageViewandroid:tint 一起使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      • 2015-12-30
      • 2021-03-11
      • 2015-07-17
      • 1970-01-01
      • 2012-10-20
      • 1970-01-01
      相关资源
      最近更新 更多