色调在DataBinding中的< 21版本不起作用 [英] Tint does not work <21 version in DataBinding

查看:128
本文介绍了色调在DataBinding中的< 21版本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用数据绑定 tint 基于boolean标志的 ImageView 的矢量可绘制对象.此代码适用于> = 21版本.但是在< 21版本中失败.

I am using DataBinding to tint vector drawable of ImageView on basic of an boolean flag. This code works well for >=21 version. But fails in <21 version.

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

此处 ic_right_blue_24dp 是可绘制的矢量.

检查绑定类之后,我可以看到未生成< 21版本的代码.

After checking binding class, I could see that code for <21 version is not getting generated.

       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 .

Here I must tell you that all things work using regular tint without binding.

推荐答案

此BindingAdapter会以编程方式设置色调...并且对我有用

This BindingAdapter will set tint programmatically...and it worked for me

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

用法

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

这篇关于色调在DataBinding中的&lt; 21版本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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