0

我想以编程方式基于标志禁用应用程序中的暗模式。我在黑暗模式的 values-night 文件夹中定义了自定义颜色。

我尝试了以下解决方案来禁用暗模式,但没有奏效。

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

有没有办法在不删除 values-night 文件夹的情况下以编程方式禁用暗模式?

4

5 回答 5

3

好像您没有定义正确的主题颜色。但请检查以下解决方案:

吨。

 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>
于 2021-06-28T11:49:53.260 回答
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>
于 2021-06-19T08:36:46.293 回答
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);
}
于 2021-06-27T06:10:56.170 回答
0

您可以使用带有 int 标识符的变量来获取 UI 模式,然后将此变量放在条件语句中以选择 UI 的状态,然后编写上面使用的行

int nightModeFlags = getContext().getResources().getConfiguration().uiMode &
Configuration.UI_MODE_NIGHT_MASK;
switch (nightModeFlags) {

  case Configuration.UI_MODE_NIGHT_YES:
     AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
     break;

  case Configuration.UI_MODE_NIGHT_NO:
     AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
     break; 
}

我希望这解释能解决你的问题

于 2021-06-26T21:11:00.783 回答
0

将暗模式设置为 false 的代码将根据两个因素起作用,无论是使用setDefaultNightMode()或设置夜间模式setLocalNightMode()。暗模式setDefaultNightMode()值将在 Activity 中被 设置的值覆盖setLocalNightMode()。因此始终建议使用本地上下文(Activity 上下文)而不是 Application 上下文来解析颜色值。

有关更多信息,请参阅本文。

DayNight - 暗模式处理

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

从 xml 解析暗模式颜色。- 参考这个链接。

于 2021-06-26T03:21:02.913 回答