【问题标题】:Compiler error when applying theme with databinding使用数据绑定应用主题时出现编译器错误
【发布时间】:2020-07-09 10:26:49
【问题描述】:

我的代码无限期地对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

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

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

Task :app:kaptDevDebugKotlin FAILED location: package com..databinding FAILURE: Build 因异常而失败。

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

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

但如果我创建了一个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 这是一个非常方便的帮助类来提供资源。

【问题讨论】:

  • 是的。这是一个数据绑定的事情。

标签: android android-databinding android-theme android-styles android-binding-adapter


【解决方案1】:

yb...@google.com #3Apr 30, 2020 16:45 状态:不会 修复(不可行) 16:45 +CC:​yb...@google.com 16:45

很遗憾,您无法通过数据绑定应用主题。他们只是 曾经读过通货膨胀时间,之后就没用了(我们没有 视图上的工作 setStheme 函数)。我们对数据无能为力 不幸的是,有约束力的一面。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-16
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    相关资源
    最近更新 更多