效果图:
本例程主要是通过下述方法来实现夜间模式切换:
需要先在res路径下创建路径:values-night,拷贝values下的colors.xml到values-night下,修改对应的色值即可。
android.support.v7.app.AppCompatDelegate.AppCompatDelegate.setDefaultNightMode(int mode);方法来设置系统默认白天与夜间模式。
调用此方法后不会立即生效,还需要调用recreate()方法后才会生效,重启Activity后会导致页面数据丢失,所以在进行切换时注意保护现场数据,以便恢复。
退出应用后,下次打开APP时,可在Application进行判断是否设置过夜间模式,再调用 AppCompatDelegate.setDefaultNightMode(int mode);
让设置在整个应用中全部生效。
本例程简单四步实现:
1、添加values-night资源文件夹,拷贝values下的colors.xml到values-night下,修改色值
(1)、values下的colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="bg">#DDDDDD</color>
<!--文字颜色-->
<color name="color_txt">#9C9C9C</color>
</resources>
(2)、values-night下的colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#333333</color>
<color name="colorPrimaryDark">#666666</color>
<color name="colorAccent">#DDDDDD</color>
<color name="bg">#666666</color>
<!--文字颜色-->
<color name="color_txt">#FFFFFF</color>
</resources>
2、点击设置按钮时调用的关键代码如下:
/**
*点击事件
**/SettingActivity
sa_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//记录设置时间
SharedPreferencesUtil.setLong(SettingsActivity.this, Constant.KEY_SYS_NIGHT_MODE_TIME, new Date().getTime());
if ((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
SharedPreferencesUtil.setBoolean(SettingsActivity.this, Constant.KEY_SYS_NIGHT_MODE, false);
} else {
//此方法只针对当前Activity有效,所以要让其他Activity生效,须调用recreate 才行
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
SharedPreferencesUtil.setBoolean(SettingsActivity.this, Constant.KEY_SYS_NIGHT_MODE, true);
}
//切换动画在7.0不生效
//getWindow().setWindowAnimations(R.style.WindowAnimationFadeInOut);
//配置夜间模式后需要调用此方法才能生效
recreate();
//EventBus通知首页更新主题
EventBus.getDefault().post(new MainEvent(1));
}
});
3、通知其他Activity同时更新:
@Subscribe
public void onEventMainThread(MainEvent event) {
if (event.getMessage() == 1) {
//通知时,如果发现本次设置夜间模式的时间与上次设置的时间不同,则进行夜间模式判断;此判断貌似有点多余。
if (SharedPreferencesUtil.hasKey(this, Constant.KEY_SYS_NIGHT_MODE_TIME) && MyApplication.sys_night_mode_time != SharedPreferencesUtil.getLong(this, Constant.KEY_SYS_NIGHT_MODE_TIME, 0)) {
if (SharedPreferencesUtil.hasKey(this, Constant.KEY_SYS_NIGHT_MODE)) {
if (SharedPreferencesUtil.getBoolean(this, Constant.KEY_SYS_NIGHT_MODE, false)) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
//切换动画在7.0不生效
//this.getWindow().setWindowAnimations(R.style.WindowAnimationFadeInOut);
recreate();
}
}
}
4、APP启动时在Application中应用设置的模式:
sys_night_mode_time = SharedPreferencesUtil.getLong(this, Constant.KEY_SYS_NIGHT_MODE_TIME, 0);
//如果切换过夜间模式那么时间不会为0,防止无用的判断
if (sys_night_mode_time != 0) {
if (SharedPreferencesUtil.getBoolean(this, Constant.KEY_SYS_NIGHT_MODE, false)) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}