【发布时间】:2019-11-13 10:10:20
【问题描述】:
我有一个用户可以选择浅色或深色的主题,一切正常,但是有两个问题:
- 当我打开应用程序[在深色/浅色主题中]时,首先显示一个白色页面,然后应用程序使用深色主题
- 当我更改主题时,首先出现黑屏
我尝试改变背景但没有用,我改变了它的颜色,还使用了可绘制的形状。无论如何,它不起作用,两者都使用一种颜色。
style.xml
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar"/>
值/主题.xml
<style name="Theme.MaterialComponents.DayNight.NoActionBar"
parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@drawable/window_background</item>
</style>
values-night/themes.xml
<style name="Theme.MaterialComponents.DayNight.NoActionBar"
parent="Theme.MaterialComponents.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/TabBackground</item>
<item name="colorAccent">@color/green_inactive</item>
<item name="android:windowBackground">@drawable/window_background_dark</item>
</style>
windows_background
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#64b5f6"/>
</shape>
windows_background_dark
和上面的代码类似,只是颜色不同
这就是我加载主题的方式
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Prefs.getIsDarkMode(getApplicationContext()))
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
else
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
setContentView(R.layout.activity_main);
}
【问题讨论】:
-
你有没有找到任何解决方案我面临同样的问题