我的代码对颜色和索引进行了洗牌,以获得一个列表。ColorBox
对象无限期。这是我的观点。
<TextView
style="@style/App.WidgetStyle.ColorBox"
android:text="@{item.id}"
android:theme="@{item.theme}"
tools:text="A"
tools:theme="@style/App.ColorBox" />
My styles:
<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工具版本4.5.3用于代码生成不匹配当前运行时版本4.7.1ANTLR运行时版本4.5.3用于解析器编译不匹配当前运行时版本4.7.1ANTLR工具版本4.5. 3用于代码生成不匹配当前运行时版本4.7.1ANTLR Runtime版本4.5.3用于解析器编译不匹配当前运行时版本4.7.1Users...DataBinderMapperImpl.java:10: error: cannot find symbol import com....RowForItemBindingImpl; ^ symbol: class RowForItemBindingImpl
Task :app:kaptDevDebugKotlin FAILED location: package com....databinding FAILURE: 构建失败,出现异常。
- 出了什么问题:任务':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
"黑客"?
还有 资源提供者 它是一个非常方便的帮助类,可以提供资源。
yb...@google.com #3#2020年4月30日 16:45状态: 不会修复(无法修复) 16:45 +yb...@google.com 16:45
不幸的是,你不能通过数据绑定来应用主题。它们只在膨胀时被读取,之后就没有用了(我们在视图上没有一个有效的setStheme函数)。