设备主题模式阻止应用程序主题模式.Android Studio Java



我开始使用这个教程在我的android应用程序中实现夜/光模式。

但是设备通过系统具有此功能。问题是系统夜间模式确实阻止了我在应用程序中的代码。这是怎么发生的??

  • 如果设备模式是暗的,但在应用程序中,背景是白色的,它不能工作。(背景为深色)
  • 如果设备模式为亮屏模式,但在app中,状态栏背景为黑色,则无法正常工作。(状态栏为白色)

如何解决?谢谢。


DayNight:

<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item>
<item name="colorPrimary">@color/colorBlack</item>
<item name="colorPrimaryDark">@color/colorBlack</item>
<item name="colorAccent">@color/colorGrey</item>
</style>
<style name="AppBottomSheetDialogTheme"
parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/AppModalStyle</item>
</style>
<style name="AppModalStyle"
parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@drawable/background_curved</item>
</style>
<style name="AppTheme1" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">@color/colorWhite</item>
<item name="colorPrimaryDark">@color/colorWhite</item>
<item name="android:buttonTint">@color/colorWhite</item>
</style>
</resources>
<resources>
<color name="colorPrimary">#fff</color>
<color name="colorPrimaryDark">#fff</color>
<color name="colorAccent">#fff</color>
<color name="colorBlack">#fff</color>
<color name="colorWhite">#000</color>
<color name="colorGrey">#808080</color>
<color name="colorBackground">#1A1A1A</color>
<color name="colorBottomMenu">#000</color>
</resources>

:

<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item>
<item name="colorPrimary">@color/colorBlack</item>
<item name="colorPrimaryDark">@color/colorBlack</item>
<item name="colorAccent">@color/colorGrey</item>
</style>
<style name="AppBottomSheetDialogTheme"
parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/AppModalStyle</item>
</style>
<style name="AppModalStyle"
parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@drawable/background_curved</item>
</style>
<style name="AppTheme1" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">@color/colorWhite</item>
<item name="colorPrimaryDark">@color/colorWhite</item>
<item name="android:buttonTint">@color/colorWhite</item>
</style>
</resources>
<resources>
<color name="colorPrimary">#000</color>
<color name="colorPrimaryDark">#000</color>
<color name="colorAccent">#000</color>
<color name="colorBlack">#000</color>
<color name="colorWhite">#fff</color>
<color name="colorGrey">#808080</color>
<color name="colorBackground">#fff</color>
<color name="colorBottomMenu">#fff</color>
</resources>

你可以在本地保存亮模式或暗模式,并在应用程序打开时检查,并可以相应地修复模式。

public class MainApp extends Application {

@Override
public void onCreate() {
super.onCreate();

//https://developer.android.com/training/data-storage/shared-preferences
boolean isNightMode = new MySharedPref(this).isDarkMode(); //sharedpreferences...

if (isNightMode) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
}

Theme.MaterialComponents.DayNight.NoActionBar是所有你需要的…

结合values/styles.xmlvalues-night/styles.xml