如何在Android Studio的布局预览中选择我的主题

3
在我的项目的res/values目录中,我有一个my_themes.xml文件,其中指定了自定义属性和两个自定义主题(浅色和深色)。这些主题通过第三个构造函数参数(在代码中)应用于Presentation对象。
当我编辑我的布局文件并查看主题预览时,我想能够选择我的两个主题中的任何一个,并看到带有主题的结果。当我运行代码时,一切都正常;这只是一个关于如何让布局预览显示我的主题的问题。
但是似乎不可能选择我的自定义主题。当我点击下拉菜单以选择要使用的主题时,只有我的“默认”AppTheme(及其父级)可见:

enter image description here

如果我点击“更多主题...”,我的自定义主题不在选项中。我在这里搜索“我的”(它们是MyLightThemeMyDarkTheme),但是没有结果:

enter image description here


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
            }
    }
}

你的主题是否在同一个模块中声明?通常情况下,如果当前模块看到它们,它们就会出现。如果它们在另一个模块中声明,并且你的模块没有依赖于该模块,那么它们将不会被看到(遗憾地)。 - cjurjiu
@cjurjiu 是的,它们都在同一个模块中(简单演示只有一个模块)。感觉像是Android Studio只会添加在清单中列出的主题样式之类的东西。 - Ben P.
1个回答

2
我无法找到一种方法使我的主题出现在下拉菜单中,但是我通过将tools:theme属性添加到布局的根视图来解决了这个问题。
例如:
tools:theme="@style/MyLightTheme"

然后,无论下拉菜单中选择了什么主题,布局预览都会使用指定的主题。

最初的回答

对我来说,我只需要切换限定词,而不是主题。在旋转图标下有一个“夜间主题”子菜单,可以让您将其更改为夜间主题。 - Kobato

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接