【问题标题】:Tint does not work <21 version in DataBinding色调在 DataBinding 中不起作用 <21 版本
【发布时间】:2018-10-29 12:44:51
【问题描述】:

我在布尔标志的基础上使用DataBindingtint 的矢量可绘制ImageView。此代码适用于 >=21 版本。但在

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

这里的ic_right_blue_24dp 是一个可绘制的矢量。

检查绑定类后,我可以看到

       if(getBuildSdkInt() >= 21) {

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

我已经尝试了所有我能想到并能找到的方法。

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

在这里我必须告诉你,使用常规 tint 而不使用 binding,所有事情都可以正常工作。

【问题讨论】:

  • 测试用例app:tint
  • 是的,我已经尝试过了,它会导致错误。 :Cannot find the setter for attribute 'app:tint' with parameter type int on androidx.appcompat.widget.AppCompatImageView.
  • 你试过这个v7/widget/AppCompatImageView吗? &lt;android.support.v7.widget.AppCompatImageView app:tint="..." ... /&gt;
  • @Mykhailo 不,我已经迁移到AndroidX,我相信AndroidXSupport 的类没有区别。因为AndroidX 只是一个重命名过程。 stackoverflow.com/q/51280090/6891563
  • 这能回答你的问题吗? Set the tint of an ImageView using databinding

标签: android android-databinding


【解决方案1】:

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

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

【讨论】:

【解决方案2】:

这个 BindingAdapter 将以编程方式设置色调...它对我有用

@BindingAdapter("android:tint")
fun AppCompatImageView.setImageTint(@ColorInt color: Int) {
    setColorFilter(color)
}

用法

<androidx.appcompat.widget.AppCompatImageView
    android:tint="@color/primary"
    ...
    />

【讨论】:

  • 它是一个 BindingAdapter,它将以编程方式设置色调......它对我有用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-31
  • 2013-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多