问题
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?
回答1:
Try this:
Make sure that the activities extends AppCompatActivity.
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">Set the theme of your Activities in your AndroidManifest to:
android:theme="@style/AppTheme"
or any theme that uses Theme.AppCompat.DayNight
来源:https://stackoverflow.com/questions/57421476/how-to-make-dark-mode-in-every-activity