直接上代码
public class DarkModeUtils {
public static final String KEY_CURRENT_MODEL = "night_mode_state_sp";
private static int getNightModel(Context context) {
SharedPreferences sp = context.getSharedPreferences(KEY_CURRENT_MODEL, Context.MODE_PRIVATE);
return sp.getInt(KEY_CURRENT_MODEL, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
public static void setNightModel(Context context, int nightMode) {
SharedPreferences sp = context.getSharedPreferences(KEY_CURRENT_MODEL, Context.MODE_PRIVATE);
sp.edit().putInt(KEY_CURRENT_MODEL, nightMode).apply();
}
/**
* ths method should be called in Application onCreate method
*
* @param application application
*/
public static void init(Application application) {
int nightMode = getNightModel(application);
AppCompatDelegate.setDefaultNightMode(nightMode);
}
/**
* 应用夜间模式
*/
public static void applyNightMode(Context context) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
setNightModel(context, AppCompatDelegate.MODE_NIGHT_YES);
}
/**
* 应用日间模式
*/
public static void applyDayMode(Context context) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
setNightModel(context, AppCompatDelegate.MODE_NIGHT_NO);
}
/**
* 跟随系统主题时需要动态切换
*/
public static void applySystemMode(Context context) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
setNightModel(context, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
/**
* 判断App当前是否处于暗黑模式状态
*
* @param context 上下文
* @return 返回
*/
public static boolean isDarkMode(Context context) {
int nightMode = getNightModel(context);
if (nightMode == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) {
int applicationUiMode = context.getResources().getConfiguration().uiMode;
int systemMode = applicationUiMode & Configuration.UI_MODE_NIGHT_MASK;
return systemMode == Configuration.UI_MODE_NIGHT_YES;
} else {
return nightMode == AppCompatDelegate.MODE_NIGHT_YES;
}
}
}
使用流程
很明显这是切换暗黑模式的方法,在调用前必选先适配App的暗黑样式。
如何适配
- 颜色资源
新建values-night文件夹,将页面中使用的色值都替换成暗黑模式下的色值。 - 图片资源
新建mipmap-night/drawable-night文件夹,将页面中使用的图片和样式资源都替换成暗黑模式下的对应资源。 - 状态栏
通过上方代码中最后一个方法isDarkMode判断显示什么颜色的状态栏。最好在BaseActivity中操作,否则Activity很多的话很麻烦。 - 调用
调用上方的方法切换App的暗黑模式。需要注意的点如下:
需要注意
- androidx上直接调用即可。support上使用并且在Activity中切换暗黑模式,需要动态调用一下activity.recreate()方法。具体原因看下面源码:
androidx版本:
/**
* Sets the default night mode. This is the default value used for all components, but can
* be overridden locally via {@link #setLocalNightMode(int)}.
*
* <p>This is the primary method to control the DayNight functionality, since it allows
* the delegates to avoid unnecessary recreations when possible.</p>
*
* <p>If this method is called after any host components with attached
* {@link AppCompatDelegate}s have been 'started', a {@code uiMode} configuration change
* will occur in each. This may result in those components being recreated, depending
* on their manifest configuration.</p>
*
* <p>Defaults to {@link #MODE_NIGHT_FOLLOW_SYSTEM}.</p>
*
* @see #setLocalNightMode(int)
* @see #getDefaultNightMode()
*/
public static void setDefaultNightMode(@NightMode int mode) {
switch (mode) {
case MODE_NIGHT_NO:
case MODE_NIGHT_YES:
case MODE_NIGHT_FOLLOW_SYSTEM:
case MODE_NIGHT_AUTO_TIME:
case MODE_NIGHT_AUTO_BATTERY:
if (sDefaultNightMode != mode) {
sDefaultNightMode = mode;
applyDayNightToActiveDelegates();
}
break;
default:
Log.d(TAG, "setDefaultNightMode() called with an unknown mode");
break;
}
}
support版本:
public static void setDefaultNightMode(int mode) {
switch(mode) {
case -1:
case 0:
case 1:
case 2:
sDefaultNightMode = mode;
break;
default:
Log.d("AppCompatDelegate", "setDefaultNightMode() called with an unknown mode");
}
}
对比后可以发现androidx切换暗黑模式后,自己主动调用了apply方法,使Activity重建。而support上没有,仅仅是赋值而已。所以support版本上使用需要自己调用activity.recreate()方法。
- 暂时没有了