當我單擊夜間模式按鈕時,它僅適用于設(shè)置活動,但其他活動不是夜間模式。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 SettingsActivitySwitch 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); } });而且當我關(guān)閉應用程序并再次打開時,夜間模式不會保存。怎么做?
1 回答

四季花海
TA貢獻1811條經(jīng)驗 獲得超5個贊
嘗試這個:
確?;顒訑U展 AppCompatActivity。
您的主題必須使用 Theme.AppCompat.DayNight 因此您應該將主主題或 AppTheme 設(shè)置為 DayNight:
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
在 AndroidManifest 中將 Activity 的主題設(shè)置為:
android:theme="@style/AppTheme"
或任何使用 Theme.AppCompat.DayNight 的主題
添加回答
舉報
0/150
提交
取消