如何在每项活动中使黑暗模式 [英] How to make dark mode in every activity
本文介绍了如何在每项活动中使黑暗模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我单击夜间模式按钮时,它仅适用于设置活动,而其他活动不是夜间模式.
When I click night mode button it only apply in settings activity but other activities are not night mode.
This is xml
<Switch
android:id="@+id/switch_button"
android:layout_marginStart="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/night"/>
This is SettingsActivity
Switch aSwitch = findViewById(R.id.switch_button);
aSwitch.setOnCheckedChangeListener((compoundButton, isChecked) -> {
if (isChecked) {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
else
{
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
});
当我关闭应用程序并再次打开时,夜间模式也不会保存.该怎么做?
And also when I close app and open again, night mode is not saved. How to do that?
推荐答案
尝试一下:
-
确保活动扩展了AppCompatActivity.
Make sure that the activities extends AppCompatActivity.
您的主题必须使用Theme.AppCompat.DayNight,因此您应将主主题或AppTheme设置为DayNight:
Your theme must use Theme.AppCompat.DayNight therefore you should set your main theme or AppTheme to DayNight:
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
在AndroidManifest中将活动"的主题设置为:
Set the theme of your Activities in your AndroidManifest to:
android:theme="@style/AppTheme"
或使用Theme.AppCompat.DayNight的任何主题
or any theme that uses Theme.AppCompat.DayNight
这篇关于如何在每项活动中使黑暗模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文