使用数据绑定应用主题时出现编译器错误

Compiler error when applying theme with databinding

我的代码无限期地为 ColorBox 个对象的列表打乱颜色和索引。
这是我的观点:

<TextView
    style="@style/App.WidgetStyle.ColorBox"
    android:text="@{item.id}"
    android:theme="@{item.theme}"
    tools:text="A"
    tools:theme="@style/App.ColorBox" />

我的风格:

<style name="App.WidgetStyle.ColorBox" parent="App">
    <item name="android:layout_width">@dimen/square_size</item>
    <item name="android:layout_height">@dimen/square_size</item>
    <item name="android:background">@drawable/shape_for_rounded_outlined_bg</item>
    <item name="android:fontFamily">@font/open_sans_bold</item>
    <item name="android:gravity">center</item>
</style>

我的自定义背景形状:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:radius="?attr/backgroundShapeCornerRadius" />
    <solid android:color="?attr/colorPrimary"/>
    <stroke android:width="1dp" android:color="?attr/colorSecondary"/>
</shape>

我的主题:

<style name="App.ColorBox">
    <item name="colorPrimary">@android:color/transparent</item>
    <item name="colorSecondary">@color/black</item>
    <item name="backgroundShapeCornerRadius">0dp</item>
</style>
<style name="App.ColorBox.Red">
    <item name="colorPrimary">@color/color_1</item>
</style>
<style name="App.ColorBox.Green">
    <item name="colorPrimary">@color/color_2</item>
</style>
<style name="App.ColorBox.White">
    <item name="colorPrimary">@color/color_3</item>
</style>
<style name="App.ColorBox.Blue">
    <item name="colorPrimary">@color/color_4</item>
</style>

我的数据class:

@Parcelize
data class ColorBox(var id: String, @StyleRes var theme: Int) : Parcelable

如果我尝试编译,编译器会讨厌它:

Task :app:kaptDevDebugKotlin ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1/Users/.../DataBinderMapperImpl.java:10: error: cannot find symbol import com....RowForItemBindingImpl; ^ symbol: class RowForItemBindingImpl

Task :app:kaptDevDebugKotlin FAILED location: package com....databinding FAILURE: Build failed with an exception.

  • 出了什么问题:任务“:app:kaptDevDebugKotlin”执行失败。

    A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution java.lang.reflect.InvocationTargetException (no error message)

但是如果我创建一个 BindingAdapter(例如 this 不起作用)

object AppBindingAdapters {

    @JvmStatic
    @BindingAdapter(value = ["colorBoxTheme"])
    fun colorBoxTheme(view: View, @StyleRes themeResId: Int) {
        view.background = ResourcesProvider(view.context).drawable(R.drawable.shape_for_rounded_outlined_bg, themeResId)
    }
}

<TextView
    style="@style/App.WidgetStyle.ColorBox"
    android:text="@{item.id}"
    app:colorBoxTheme="@{item.theme}"
    tools:text="A"
    tools:theme="@style/App.ColorBox.Green" />

有效:)

这是 databinding 错误还是预期的行为?为什么我不能在没有 BindingAdapter "hack" 的情况下使用 databinding 动态应用主题?

顺便说一句,ResourcesProvider它是提供资源的非常方便的帮手class。

yb...@google.com #3Apr 30, 2020 16:45 Status: Won't Fix (Infeasible) 16:45 +CC:​yb...@google.com 16:45

you cannot apply themes via data binding unfortunately. they are only ever read on inflation time and useless afterwards (we don't have a working setStheme function on views). nothing we can do on the data binding side unfortunately.

https://issuetracker.google.com/issues/152712592