android - 色调在 DataBinding 中不起作用 <21 版本

标签 android android-databinding

我正在使用 DataBinding在 bool 标志的基础上为 ImageViewtint 矢量绘图。此代码适用于 >=21 版本。但在 <21 版本中失败。

<androidx.appcompat.widget.AppCompatImageView
    android:tint="@{model.nextEnabled ? @color/primary : @color/silver}"
    app:srcCompat="@drawable/ic_right_blue_24dp"
    />

这里 ic_right_blue_24dp 是一个矢量图。

检查绑定(bind)类后,我可以看到 <21 版本的代码没有生成。

       if(getBuildSdkInt() >= 21) {

            this.mboundView1.setImageTintList(androidx.databinding.adapters.Converters.convertColorToColorStateList(modelBackEnabledMboundView1AndroidColorPrimaryMboundView1AndroidColorSilver));
        }

我已经尝试了所有我能想到并能找到的东西。

  • AppCompatImageView
  • ImageView
  • app:srcCompat
  • android:src
  • app:tint
  • vectorDrawables.useSupportLibrary = true
  • AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

在这里我必须告诉你所有的东西都可以使用常规的 tint 而不需要 binding

最佳答案

还有custom data-binding .甚至完全按照要求的方法:

@BindingMethods({
    @BindingMethod(
        type = "androidx.appcompat.widget.AppCompatImageView",
        attribute = "android:tint",
        method = "setImageTintList"
    )
})

关于android - 色调在 DataBinding 中不起作用 <21 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53045802/

相关文章:

android - 如何在 TextView 中为文本制作边框?

android - 如何让两种构建风格继承自 Android Studio 中的根风格?

android - GoogleServices - 如何使用 flavorDimensions 为每个 flavor 组合设置不同的 applicationId?

android-databinding - 安卓数据绑定(bind): how to pass variable to include layout

android - 双向数据绑定(bind)android上编辑文本的布局错误

java - android studio 中的 Tabitem 绑定(bind)

android - 如何提前终止 Toast 消息?

android - 让 admob 高于 actionbarsherlock

android - 启用 viewBinding 功能失败(Android Studio 3.6)

android - MVVM : How to Concat the String in model class?