我希望能够根据一个标志程序性地禁用应用程序中的暗黑模式。我已经在values-night文件夹中定义了自定义颜色以适应暗黑模式。
我尝试了以下解决方案以禁用暗黑模式,但没有成功:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
是否有任何方法可以在不删除values-night文件夹的情况下以编程方式禁用暗黑模式?
我希望能够根据一个标志程序性地禁用应用程序中的暗黑模式。我已经在values-night文件夹中定义了自定义颜色以适应暗黑模式。
我尝试了以下解决方案以禁用暗黑模式,但没有成功:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
是否有任何方法可以在不删除values-night文件夹的情况下以编程方式禁用暗黑模式?
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>
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);
}
private UiModeManager uiModeManager;
uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
uiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO);
或者您可以在样式部分添加以下内容
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
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);
}
你可以在 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文件中进行更改。
将暗模式设置为false的代码将基于两个因素工作,即是否使用setDefaultNightMode()
或 setLocalNightMode()
设置了夜间模式。活动中的暗模式setDefaultNightMode()
值将被setLocalNightMode()
设置的值覆盖。因此,始终建议使用本地上下文(Activity context)解析颜色值,而不是应用程序上下文。
请参阅此文章以获取更多信息。
确保在解析颜色时使用正确的上下文。如果您使用应用程序上下文解析颜色,则其值将与使用 Activity 上下文解析的值不同。
Resolving dark mode colors from xml. - 请参考此链接。
parent="Theme.MaterialComponents.Light.DarkActionBar"
- IntelliJ Amiya