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的暗黑样式。
如何适配
- 颜色资源
新建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()方法。
总结
到此这篇关于Android如何快速适配暗黑模式的文章就介绍到这了,更多相关Android适配暗黑模式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Android TableLayout数据列表的回显清空实现思路及代码
数据列表的回显必须从后面减去子元素同时必须从后面减去子元素,感兴趣的朋友可以看下具体的实现代码,希望对你学习Android TableLayout有所帮助2013-04-04Android Studio配置国内镜像源(利用hosts)
这篇文章主要介绍了Android Studio配置国内镜像源(利用hosts),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-11-11Android App开发的自动化测试框架UI Automator使用教程
UI Automator为Android程序的UI开发提供了测试环境,这里我们就来看一下Android App开发的自动化测试框架UI Automator使用教程,需要的朋友可以参考下2016-07-07Android 自定义View时使用TypedArray配置样式属性详细介绍
这篇文章主要介绍了Android 自定义View时使用TypedArray配置样式属性详细介绍的相关资料,需要的朋友可以参考下2016-11-11Android中判断有无可用网络的代码(是否是3G或者WIFI网络)
在android开发中经常会遇到的判断有无可用网络的代码,防止客户流量损失2013-01-01
最新评论