如何切换到深色主题?

问题描述 投票:0回答:1

我需要在应用程序中切换到深色主题,我这样做:

private fun onOffDarkTheme() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            when (requireActivity().resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
                Configuration.UI_MODE_NIGHT_YES -> {
                    prefs!!.edit().putBoolean(MODE_NIGHT, false).apply()
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
                    startActivity(Intent(requireActivity(), MainActivity::class.java))
                }
                Configuration.UI_MODE_NIGHT_NO -> {
                    prefs!!.edit().putBoolean(MODE_NIGHT, true).apply()
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
                    startActivity(Intent(requireActivity(), MainActivity::class.java))
                }
            }
        } else {
            val uiModeManager: UiModeManager = requireActivity().getSystemService(UI_MODE_SERVICE) as UiModeManager
            when (requireActivity().resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
                Configuration.UI_MODE_NIGHT_YES -> {
                    prefs!!.edit().putBoolean(MODE_NIGHT, false).apply()
                    uiModeManager.nightMode = UiModeManager.MODE_NIGHT_NO
                    startActivity(Intent(requireActivity(), MainActivity::class.java))
                }
                Configuration.UI_MODE_NIGHT_NO -> {
                    prefs!!.edit().putBoolean(MODE_NIGHT, true).apply()
                    uiModeManager.nightMode = UiModeManager.MODE_NIGHT_YES
                    startActivity(Intent(requireActivity(), MainActivity::class.java))
                }
            }
        }
    }

在应用程序中,我检查

 private fun isDarkThemeOn() {
        val uiModeManager: UiModeManager = this.getSystemService(UI_MODE_SERVICE) as UiModeManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            if (prefs!!.getBoolean(Constants.MODE_NIGHT, false)) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
            } else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
            }
        } else {
            if (prefs!!.getBoolean(Constants.MODE_NIGHT, false)) {
                uiModeManager.nightMode = UiModeManager.MODE_NIGHT_YES
            } else {
                uiModeManager.nightMode = UiModeManager.MODE_NIGHT_NO
            }
        }
    }

问题是资源(.mp4)不是从 raw-night 文件夹中提取的,它们保持不变,但如果我切换设置,那么一切都会正常工作。我知道我混淆了黑暗主题和黑暗模式,但我不知道如何解决它。我的主旋律,继承自

parent="Theme.AppCompat.DayNight"
android kotlin android-theme darkmode android-dark-theme
1个回答
0
投票

检查了字符串资源,它对我有用

实施深色主题

关于setApplicationNightMode

private fun setAppNightMode(context: Context, isDarkMode: Boolean) {
    if (VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        val uiModeManager = context.applicationContext
            .getSystemService(Context.UI_MODE_SERVICE) as UiModeManager
        val mode = if (isDarkMode ) {
            UiModeManager.MODE_NIGHT_YES
        } else {
            UiModeManager.MODE_NIGHT_NO
        }
        uiModeManager.setApplicationNightMode(mode)
    } else {
        val mode = if (isDarkMode) {
            AppCompatDelegate.MODE_NIGHT_YES
        }  else {
            AppCompatDelegate.MODE_NIGHT_NO
        }
        AppCompatDelegate.setDefaultNightMode(mode)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.