Android进阶之路Android技术知识Android开发

Android 10 (API 29)深色主题背景

2019-12-23  本文已影响0人  caigp

Android官网说的优势

我觉得在amoled屏上用起来不错(其实我没有~),经过真机的测试,android 9 MIUI上面的深色模式也可以用,我都搞不懂什么情况了~~

  1. 使用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也能用

  1. 那另外一种情况就是我字体颜色或背景已经指定颜色了,上面的可能不会生效,另一种方式就是去监听系统主题的改变

<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;
        }
 }

然后根据相应的模式去做适配就行了

上一篇 下一篇

猜你喜欢

热点阅读