我正在使用 DataBinding在 bool 标志的基础上为 ImageView
的 tint
矢量绘图。此代码适用于 >=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/