0

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>

我的数据类:

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

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

任务 :app:kaptDevDebugKotlin 用于代码生成的 ANTLR 工具版本 4.5.3 与当前运行时版本不匹配 4.7.1ANTLR 用于解析器编译的运行时版本 4.5.3 与当前运行时版本不匹配 4.7.1 使用的ANTLR 工具版本 4.5.3用于代码生成与当前运行时版本不匹配 4.7.1ANTLR 用于解析器编译的运行时版本 4.5.3 与当前运行时版本不匹配 4.7.1/Users/.../DataBinderMapperImpl.java:10: 错误:找不到符号导入 com....RowForItemBindingImpl; ^ 符号:类 RowForItemBindingImpl

任务:app:kaptDevDebugKotlin 失败位置:包 com....数据绑定失败:构建失败并出现异常。

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

    执行 org.jetbrains.kotlin.gradle.internal.KaptExecution java.lang.reflect.InvocationTargetException 时发生故障(无错误消息)

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

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错误还是期望的行为?为什么我不能在databinding没有BindingAdapter“hack”的情况下动态应用主题?

顺便说一句,ResourcesProvider它是一个非常方便的帮助类来提供资源。

4

1 回答 1

0

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

不幸的是,您不能通过数据绑定应用主题。它们只会在通货膨胀时间上阅读,之后就没有用了(我们在视图上没有工作 setStheme 功能)。不幸的是,我们在数据绑定方面无能为力。

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

于 2020-04-30T15:48:32.570 回答