Android 设置app深色、浅色、跟随系统

Android深色模式适配

我们需要再用户设置时候,记录下来,用户的设置,等app再次启动时候,获取之前设置,重新设置

    public static void setThemeMode() {
        int themeModeType = SpUtils.getThemeModeType();
        if (themeModeType == 1) {
            //1:浅色
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        } else if (themeModeType == 2) {
            // 2:深色
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            //跟随系统
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
        }
    }

1、设置单个Activity

   /**
     * 设置单个Activity 深色、浅色、跟随系统
     *
     * @param appCompatDelegate Activity AppCompatDelegate
     * @param themeModeType  0:跟随系统 1:浅色 2:深色
     */
    public static void setThemeModeByActivity(AppCompatDelegate appCompatDelegate, int themeModeType) {
        if (appCompatDelegate != null) {
            switch (themeModeType) {
                case 0:
                    appCompatDelegate.setLocalNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
                    break;
                case 1:
                    appCompatDelegate.setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    break;
                case 2:
                    appCompatDelegate.setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    break;
            }
        }
    }

2、获取当前Activity是否开启深色

   /**
     * 通过Activity Resources 获取当前是否开启深色模式
     *
     * @param object
     * @return
     */
    public static boolean nightModeByUiResources(Object object) {
        if (object != null) {
            if (object instanceof Activity) {
                int currentNightMode = ((Activity) object).getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
                return currentNightMode == Configuration.UI_MODE_NIGHT_YES;
            } else if (object instanceof androidx.fragment.app.Fragment) {
                int currentNightMode = ((Fragment) object).getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
                return currentNightMode == Configuration.UI_MODE_NIGHT_YES;
            } else if (object instanceof android.app.Fragment) {
                int currentNightMode = ((android.app.Fragment) object).getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
                return currentNightMode == Configuration.UI_MODE_NIGHT_YES;
            }
        }

        return false;
    }

3、webView设置

   /**
     * 设置webView 深色或者浅色模式
     *
     * @param activity
     * @param webSetting
     */
    public static void setWebViewNight(Activity activity, WebSettings webSetting) {
        if (activity == null || webSetting == null) {
            return;
        }
        setWebViewNight(webSetting, nightModeByUiResources(activity));

    }

    /**
     * 设置webView 暗黑模式
     *
     * @param webSetting
     * @param nightMode true:深色  false:浅色
     */
    public static void setWebViewNight(WebSettings webSetting, boolean nightMode) {
        boolean featureSupported = false;
        try {
            featureSupported = WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK);
        } catch (AbstractMethodError e) {
            e.printStackTrace();
        }
        if (featureSupported) {
            if (nightMode) {
                //启用 webview 的强制黑暗模式,这意味着 webview 的内容将始终以黑暗主题呈现
                WebSettingsCompat.setForceDark(webSetting, WebSettingsCompat.FORCE_DARK_ON);
            } else {
                //禁用 webview 的强制暗模式,这意味着 webview 的内容将按原样呈现
                WebSettingsCompat.setForceDark(webSetting, WebSettingsCompat.FORCE_DARK_OFF);

            }
        }
    }

但是h5页面需要做特别判断才能拿到webView深浅模式

@media (prefers-color-scheme: dark) {这里是样式代码}

4、深色浅色切换时候,重启app

  /**
     * 浅色和深色模式切换,杀死进程,重新打开app
     *
     * @param activity
     */
    public static void restartApp(Activity activity) {
        Intent intent = activity.getPackageManager().getLaunchIntentForPackage(activity.getPackageName());
        if (intent != null) {
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            activity.startActivity(intent);
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值