Android官网说的优势
- 可大幅减少耗电量(具体取决于设备的屏幕技术)。
- 为弱视以及对强光敏感的用户提高可视性。
-
让所有人都可以在光线较暗的环境中更轻松地使用设备。
untitled.gif
我觉得在amoled屏上用起来不错(其实我没有~),经过真机的测试,android 9 MIUI上面的深色模式也可以用,我都搞不懂什么情况了~~
- 使用Force Dark自动适配,主要适合你代码里字体或背景没有使用硬编码(就是直接指定了字体颜色或背景颜色)
直接在主题文件指定
<item name="android:forceDarkAllowed">true</item>
就像这样
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:forceDarkAllowed">true</item>
</style>
这样你就发现系统启用深色模式时,app能够自动切换,Force Dark 是api 29增加的,我发现在MIUI上面的Android 9也能用
- 那另外一种情况就是我字体颜色或背景已经指定颜色了,上面的可能不会生效,另一种方式就是去监听系统主题的改变
<activity>标签配置
android:configChanges="uiMode"
当系统背景改变时回调此方法
@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int currentNightMode = newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
Log.d(TAG, "light theme");
// Night mode is not active, we're using the light theme
break;
case Configuration.UI_MODE_NIGHT_YES:
Log.d(TAG, "dark theme");
// Night mode is active, we're using dark theme
break;
}
}
然后根据相应的模式去做适配就行了
网友评论