在我的项目的
当我编辑我的布局文件并查看主题预览时,我想能够选择我的两个主题中的任何一个,并看到带有主题的结果。当我运行代码时,一切都正常;这只是一个关于如何让布局预览显示我的主题的问题。
但是似乎不可能选择我的自定义主题。当我点击下拉菜单以选择要使用的主题时,只有我的“默认”
主题化的
res/values
目录中,我有一个my_themes.xml
文件,其中指定了自定义属性和两个自定义主题(浅色和深色)。这些主题通过第三个构造函数参数(在代码中)应用于Presentation
对象。当我编辑我的布局文件并查看主题预览时,我想能够选择我的两个主题中的任何一个,并看到带有主题的结果。当我运行代码时,一切都正常;这只是一个关于如何让布局预览显示我的主题的问题。
但是似乎不可能选择我的自定义主题。当我点击下拉菜单以选择要使用的主题时,只有我的“默认”
AppTheme
(及其父级)可见:
如果我点击“更多主题...”,我的自定义主题不在选项中。我在这里搜索“我的”(它们是MyLightTheme
和MyDarkTheme
),但是没有结果:
my_themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="MyCustomAttr" format="reference"/>
<attr name="MySecondCustomAttr" format="reference"/>
<style name="MyLightTheme" parent="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen">
<item name="MyCustomAttr">@drawable/light_thing</item>
<item name="MySecondCustomAttr">@drawable/second_light_thing</item>
</style>
<style name="MyDarkTheme" parent="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen">
<item name="MyCustomAttr">@drawable/dark_thing</item>
<item name="MySecondCustomAttr">@drawable/second_dark_thing</item>
</style>
</resources>
主题化的
Presentation
子类:open class MyThemedPresentation(outerContext: Context?, display: Display?, isLight: Boolean)
: Presentation(outerContext, display, getTheme(isLight)) {
companion object {
@StyleRes
@JvmStatic
fun getTheme(isLight: Boolean): Int =
when (isLight) {
true -> R.style.MyLightTheme
false -> R.style.MyDarkTheme
}
}
}