直接上代码 public class DarkModeUtils { public static final String KEY_CURRENT_MODEL = "n
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();
}
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);
}
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版本:
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()方法。
到此这篇关于Android如何快速适配暗黑模式的文章就介绍到这了,更多相关Android适配暗黑模式内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Android如何快速适配暗黑模式详解
本文链接: https://www.lsjlt.com/news/132316.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0