我正在使用Theme.AppCompat.DayNight
添加的新Android Support Library 23.2
在Android 5.1它工作得很好。
在Android 6.0,活动看起来像利用光的主题,但对话看起来使用黑暗的主题。
我的应用程序类:
public class MyApplication extends Application {
static {
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_YES);
}
}
我styles.xml
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="Dialog.Alert" parent="Theme.AppCompat.DayNight.Dialog.Alert"/>
我的代码显示一个对话框:
new AlertDialog.Builder(mContext, R.style.Dialog_Alert)
.setTitle("Title")
.setMessage("Message")
.show();
最好的解决办法是更新适当配置环境。下面是我做的一个片段:
public Context setupTheme(Context context) {
Resources res = context.getResources();
int mode = res.getConfiguration().uiMode;
switch (getTheme(context)) {
case DARK:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
mode = Configuration.UI_MODE_NIGHT_YES;
break;
case LIGHT:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
mode = Configuration.UI_MODE_NIGHT_NO;
break;
default:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
break;
}
Configuration config = new Configuration(res.getConfiguration());
config.uiMode = mode;
if (Build.VERSION.SDK_INT >= 17) {
context = context.createConfigurationContext(config);
} else {
res.updateConfiguration(config, res.getDisplayMetrics());
}
return context;
}
然后使用上下文中的应用,像这样
@Override
protected void attachBaseContext(Context base) {
Context context = ThemePicker.getInstance().setupTheme(base);
super.attachBaseContext(context);
}
谷歌有解决它支持23.2.1
老答案:
在Android 6.0系统的夜间模式设置defalut是UiModeManager.MODE_NIGHT_NO
,被称为前Resources.Configuration.uiMode
它会改变onCreate
。然而,支持库在onCreate
AppCompatActivity
应用其夜间模式设置,一切都太迟了,我想这就是为什么它不能在6.0工作。
所以,如果我们能在getResources()
覆盖AppCompatActivity
和改变uiMode
。
老答案:
下面是代码来解决不是在Android 6.0的工作
public class Application extends android.app.Application {
static {
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_);
}
@Override
public void onCreate() {
super.onCreate();
// add this code for 6.0
// DO NOT DO THIS. It will trigger a system wide night mode.
// This is the old answer. Just update appcompat.
// UiModeManager uiManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);
// uiManager.setNightMode(UiModeManager.MODE_NIGHT_);
}
}
注意:如果您的应用程序没有位置权限,应用程序不会有系统的同一计算结果。这意味着可以支持库认为它是夜间现在当系统没有,这样会导致你的一些UI看起来暗一些轻。
最好的办法是等待谷歌修复它。
getDelegate().applyDayNight();
后添加setDefaultNightMode
。
报上https://code.google.com/p/android/issues/detail?id=201910这个问题
但是Android支持库,修订23.2.1(2016年3月)的发布之后。此问题已得到解决。
修正了夜间模式和API级别23的兼容性问题
更新支持图书馆Android Support Library to 23.2.1
只是在你的价值观,V21添加此
<style name="Theme.AppCompat.DayNight">
工作为我所做的。
截至目前,不需要依赖摇篮,使比androidx.appcompat:appcompat:1.0.2
将已经存在其他的夜间模式。确保从Theme.AppCompat.Light.DarkActionBar
更改默认主题在styles.xml文件Theme.AppCompat.DayNight.DarkActionBar
然后做AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
切换到夜间模式。我已经在APIv23(的Android 6.0)和上述测试,它工作正常。为了更好地解释见this codelab by Android