在Android中以编程方式禁用暗黑模式

3

我希望能够根据一个标志程序性地禁用应用程序中的暗黑模式。我已经在values-night文件夹中定义了自定义颜色以适应暗黑模式。

我尝试了以下解决方案以禁用暗黑模式,但没有成功:

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

是否有任何方法可以在不删除values-night文件夹的情况下以编程方式禁用暗黑模式?

6个回答

4
似乎您没有定义正确的主题颜色。但是请参考以下解决方案:

Kt.

 val mode = if ((resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) ==
                    Configuration.UI_MODE_NIGHT_NO
                ) {
                    AppCompatDelegate.MODE_NIGHT_YES
                } else {
                    AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY
                }

            AppCompatDelegate.setDefaultNightMode(mode)

颜色.xml

创建一个独立的 颜色 文件,根目录为 /values-night/color.xml,并使用新的夜间主题颜色代码定义在 /values/color.xml 中已定义过的所有颜色。

主题.xml

/values/themes.xml 中定义您的样式:

<style name="Theme.VehicleApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_700</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/white</item>

    <!-- All your theme stuff -->

    </style>

/values-night/themes.xml中,仅定义您想在夜间模式下更改外观的样式,否则不要定义已在values/themes.xml中定义的相同样式字段,其余的颜色操作工作将由颜色文件自身完成。
   <style name="Theme.VehicleApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    
    <!-- Customize your theme here. -->

   </style>

1
每次你想要从亮模式切换到夜间模式时,你都需要调用(现在不需要了,只需调用setDefaultNightMode即可)。
//setContentView(R.layout.YOUR_LAYOUT_NAME_FOR_THE_ACTIVITY); //this is also called in the onCreate() method

所以这些函数会改变主题:

private void setThemeDark(){
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    //setContentView(R.layout.YOUR_LAYOUT_NAME_FOR_THE_ACTIVITY);
}

private void setThemeLight(){
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    //setContentView(R.layout.YOUR_LAYOUT_NAME_FOR_THE_ACTIVITY);
}

从AppCompat v1.1.0-alpha05开始,setDefaultNightMode()将自动应用任何DayNight更改到任何“已启动”的Activity。这意味着当您调用API时,您不再需要手动重新创建任何Activity。

0

阅读setNightMode

private UiModeManager uiModeManager;

uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE); 
uiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO);

或者您可以在样式部分添加以下内容

<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

两个都对我没用。在暗模式下,它从values-night文件夹中获取颜色。 - Shyam
1
@Shyam 设置你的主题 parent="Theme.MaterialComponents.Light.DarkActionBar" - IntelliJ Amiya
将其设置为@style/Theme.AppCompat.Light并尝试。但是没有成功。 - Shyam
@Shyam 你好,你是在使用MaterialComponents库还是默认的库? - Ole Pannier

0
每次你想要从白天模式切换到夜间模式时,你都需要调用。
setContentView(R.layout.YOUR_LAYOUT_NAME_FOR_THE_ACTIVITY); //this is also called in the onCreate() method

所以这些函数会改变主题:

private void setThemeDark(){
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    setContentView(R.layout.YOUR_LAYOUT_NAME_FOR_THE_ACTIVITY);
}

private void setThemeLight(){
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    setContentView(R.layout.YOUR_LAYOUT_NAME_FOR_THE_ACTIVITY);
}

0

你可以在 styles.xml 文件中进行更改。

来自:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:forceDarkAllowed">false</item>
</style>

至:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:forceDarkAllowed">false</item>
</style>

请确保您在styles文件夹下的两个styles.xml文件中进行更改。


0

将暗模式设置为false的代码将基于两个因素工作,即是否使用setDefaultNightMode() setLocalNightMode()设置了夜间模式。活动中的暗模式setDefaultNightMode()值将被setLocalNightMode()设置的值覆盖。因此,始终建议使用本地上下文(Activity context)解析颜色值,而不是应用程序上下文。

请参阅此文章以获取更多信息。

DayNight - Dark mode handling

确保在解析颜色时使用正确的上下文。如果您使用应用程序上下文解析颜色,则其值将与使用 Activity 上下文解析的值不同。

Resolving dark mode colors from xml. - 请参考此链接。


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