我是靠谱客的博主 壮观猎豹,这篇文章主要介绍Android如何快速适配暗黑模式详解,现在分享给大家,希望可以做个参考。

直接上代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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的暗黑样式。

如何适配

  1. 颜色资源
    新建values-night文件夹,将页面中使用的色值都替换成暗黑模式下的色值。
  2. 图片资源
    新建mipmap-night/drawable-night文件夹,将页面中使用的图片和样式资源都替换成暗黑模式下的对应资源。
  3. 状态栏
    通过上方代码中最后一个方法isDarkMode判断显示什么颜色的状态栏。最好在BaseActivity中操作,否则Activity很多的话很麻烦。
  4. 调用
    调用上方的方法切换App的暗黑模式。需要注意的点如下:

需要注意

androidx上直接调用即可。support上使用并且在Activity中切换暗黑模式,需要动态调用一下activity.recreate()方法。具体原因看下面源码:

androidx版本:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/** * 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版本:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
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如何快速适配暗黑模式详解的全部内容,更多相关Android如何快速适配暗黑模式详解内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(619)

评论列表共有 0 条评论

立即
投稿
返回
顶部