首页 > 解决方案 > 如何根据设备的暗模式选择设置主题?

问题描述

我在styles.xml 中设置了两个主题。当应用程序启动时,我想根据设备的暗模式选择设置适当的主题。假设我想在暗/亮模式下为 windowBackground 设置不同的颜色。

(初始窗口背景颜色是在 React 加载之前设置的,所以应该在 Android 端完成。)

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">#ff0000</item>
    </style>

    <style name="AppTheme.Dark" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">#00ff00</item>
    </style>

</resources>

标签: androidreact-nativeandroid-dark-theme

解决方案


只需在 android/app/src/main/res/ 创建一个 values-night 文件夹并在其中放置另一个带有深色主题的 styles.xml 即可解决我的问题。

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">#00ff00</item>
    </style>

</resources>

推荐阅读