1

使用时

getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK

检查应用程序当前处于什么模式,

int currentNightMode = getResources().getConfiguration().uiMode
        & Configuration.UI_MODE_NIGHT_MASK
switch (currentNightMode) {
    case Configuration.UI_MODE_NIGHT_NO:
        // Night mode is not active, we're in day time
    case Configuration.UI_MODE_NIGHT_YES:
        // Night mode is active, we're at night!
    case Configuration.UI_MODE_NIGHT_UNDEFINED:
        // We don't know what mode we're in, assume notnight
}

如果AppCompatDelegate.MODE_NIGHT_YES早先调用它

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

是回归currentNightModeConfiguration.UI_MODE_NIGHT_YES

AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM之前设置时它会返回什么

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);

并且设备已从亮变为暗(或从暗变为亮)?

4

2 回答 2

0
context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES

告诉当前应用程序将处于什么模式。

什么时候

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)

如果在设置中更改系统主题(在 Android Q 中),configuration.uiMode将反映更改。

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
or
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

注意:configuration.uiMode更改将触发配置更改,并可能导致重新创建活动。

于 2020-02-06T14:27:03.377 回答
0
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        themeSystem.setVisibility(View.VISIBLE);
    } else {
        themeSystem.setVisibility(View.GONE);
    }
    switch (sharePref.getTheme()) {
        case THEME_LIGHT:
            themeLight.setChecked(true);
            break;
        case THEME_DARK:
            themeDark.setChecked(true);
            break;
        case THEME_SYSTEM:
            themeSystem.setChecked(true);
            break;

        default:
            switch (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) {
                case Configuration.UI_MODE_NIGHT_NO:
                    themeLight.setChecked(true);
                    break;
                case Configuration.UI_MODE_NIGHT_YES:
                    themeDark.setChecked(true);
                    break;
                case Configuration.UI_MODE_NIGHT_UNDEFINED:
                    themeLight.setChecked(true);
                    break;
            }
    }


    themeGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            switch (checkedId) {
                case R.id.themeLight:
                    setTheme(AppCompatDelegate.MODE_NIGHT_NO, THEME_LIGHT);
                    break;
                case R.id.themeDark:
                    setTheme(AppCompatDelegate.MODE_NIGHT_YES, THEME_DARK);
                    break;
                case R.id.themeSystem:
                    setTheme(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM, THEME_SYSTEM);
                    break;
            }
        }
    });
于 2021-03-13T09:15:26.293 回答