时间:2022-03-14 10:04:42 | 栏目:Android代码 | 点击:次
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的暗黑样式。
如何适配
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()方法。