以编程方式关闭设备上的暗模式。 (或其他解决方案)Android studio Kotlin

Turn off dark mode on device programatically. (or other solution) Android studio Kotlin

所以我需要一点帮助。

我的问题是,当我的应用程序中有一个更改为暗模式的功能时,当我在设备处于亮模式时更改它时,它会很好地更改,但是当我更改它时设备处于处于深色模式,它不会改变某些东西(背景和文字)。

我找到了一个解决方案,当我在设备选项中禁用黑暗模式时,它工作正常。我只想在应用程序中更改应用程序主题时更改应用程序主题,而不是在更改设备主题时更改应用程序主题,因为它会毁了它。这个问题没有发生在 Pixel 4 模拟器上,只发生在我的 POCO F2 Pro MIUI 12.5.5 phone 上。

themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Trieskask" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/primaryLightColor</item>
    <item name="colorPrimaryVariant">@color/primaryColor</item>
    <item name="colorAccent">@color/accentColorLight</item>
    <item name="colorOnPrimary">@color/primaryTextColor</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/secondaryLightColor</item>
    <item name="colorSecondaryVariant">@color/secondaryColor</item>
    <item name="colorOnSecondary">@color/secondaryTextColor</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:fitsSystemWindows">true</item>

    <item name="bottomNavigationStyle">@style/Widget.App.BottomNavigationView</item>
</style>

<!-- BottomNav style -->
<style name="Widget.App.BottomNavigationView" parent="Widget.MaterialComponents.BottomNavigationView.Colored">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.BottomNavigationView</item>
</style>

<style name="ThemeOverlay.App.BottomNavigationView" parent="">
    <item name="colorPrimary">@color/secondaryLightColor</item>
    <item name="colorOnPrimary">@color/primaryTextColor</item>
</style>

<!-- Cards style -->
<style name="Widget.App.CardView" parent="Widget.MaterialComponents.CardView">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.Card</item>
    <item name="shapeAppearance">@style/ShapeAppearance.App.MediumComponent</item>
</style>

<style name="ThemeOverlay.App.Card" parent="">
    <item name="colorSurface">@color/primaryColor</item>
    <item name="colorOnSurface">@color/primaryTextColor</item>
</style>

<style name="ShapeAppearance.App.MediumComponent" parent="ShapeAppearance.MaterialComponents.MediumComponent">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">8dp</item>
</style>

<!-- Appbar style -->
<style name="Widget.App.Toolbar" parent="Widget.MaterialComponents.Toolbar.Primary">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.Toolbar</item>
    <item name="titleTextAppearance">@color/primaryTextColor</item>
    <item name="subtitleTextAppearance">@color/primaryTextColor</item>
</style>

<style name="ThemeOverlay.App.Toolbar" parent="">
    <item name="colorPrimary">@color/secondaryLightColor</item>
    <item name="colorPrimaryVariant">@color/secondaryLightColor</item>
    <item name="colorOnPrimary">@color/primaryTextColor</item>
</style>

<style name="Widget.App.MaterialDivider" parent="Widget.MaterialComponents.MaterialDivider">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.MaterialDivider</item>
</style>

<!-- Divider style -->
<style name="ThemeOverlay.App.MaterialDivider" parent="">
    <item name="colorOnSurface">@color/primaryTextColor</item>
</style>

night/themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Trieskask" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/primaryDarkColor</item>
    <item name="colorPrimaryVariant">@color/primaryColor</item>
    <item name="colorAccent">@color/accentColorDark</item>
    <item name="colorOnPrimary">@color/secondaryTextColor</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/secondaryDarkColor</item>
    <item name="colorSecondaryVariant">@color/secondaryColor</item>
    <item name="colorOnSecondary">@color/primaryTextColor</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:fitsSystemWindows">true</item>
</style>

<!-- BottomNav style -->
<style name="Widget.App.BottomNavigationView" parent="Widget.MaterialComponents.BottomNavigationView.Colored">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.BottomNavigationView</item>
</style>

<style name="ThemeOverlay.App.BottomNavigationView" parent="">
    <item name="colorPrimary">@color/secondaryDarkColor</item>
    <item name="colorOnPrimary">@color/secondaryTextColor</item>
</style>

<!-- Cards style -->
<style name="Widget.App.CardView" parent="Widget.MaterialComponents.CardView">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.Card</item>
    <item name="shapeAppearance">@style/ShapeAppearance.App.MediumComponent</item>
</style>

<style name="ThemeOverlay.App.Card" parent="">
    <item name="colorSurface">@color/secondaryColor</item>
    <item name="colorOnSurface">@color/secondaryTextColor</item>
</style>

<style name="ShapeAppearance.App.MediumComponent" parent="ShapeAppearance.MaterialComponents.MediumComponent">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">8dp</item>
</style>

<!-- Appbar style -->
<style name="Widget.App.Toolbar" parent="Widget.MaterialComponents.Toolbar.Primary">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.Toolbar</item>
    <item name="titleTextAppearance">@color/secondaryTextColor</item>
    <item name="subtitleTextAppearance">@color/secondaryTextColor</item>
</style>

<style name="ThemeOverlay.App.Toolbar" parent="">
    <item name="colorPrimary">@color/secondaryDarkColor</item>
    <item name="colorPrimaryVariant">@color/secondaryDarkColor</item>
    <item name="colorOnPrimary">@color/secondaryTextColor</item>
</style>

<style name="Widget.App.MaterialDivider" parent="Widget.MaterialComponents.MaterialDivider">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.MaterialDivider</item>
</style>

<!-- Divider style -->
<style name="ThemeOverlay.App.MaterialDivider" parent="">
    <item name="colorOnSurface">@color/secondaryTextColor</item>
</style>

MainActivity.kt(发生暗模式和亮模式切换的地方

class MainActivity : BaseActivity() {

private lateinit var binding: ActivityMainBinding

private val profileFragment = ProfileFragment()
private val productsFragment = ProductsFragment()
private val aboutFragment = AboutFragment()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    replaceFragment(productsFragment)

    binding.bottomNavigation.menu.getItem(1).isChecked = true

    binding.bottomNavigation.setOnNavigationItemSelectedListener {
        when(it.itemId) {
            R.id.page_1 -> replaceFragment(profileFragment)
            R.id.page_2 -> replaceFragment(productsFragment)
            R.id.page_3 -> replaceFragment(aboutFragment)
        }
        true
    }
    val appSettingsPrefs: SharedPreferences = getSharedPreferences("AppSettingsPrefs", 0)
    val sharedPrefsEdit: SharedPreferences.Editor = appSettingsPrefs.edit()
    val isNightModeOn: Boolean = appSettingsPrefs.getBoolean("NightMode", false)

    if (isNightModeOn) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
        binding.switchBtn.setBackgroundResource(R.drawable.ic_sun)
    } else {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
        binding.switchBtn.setBackgroundResource(R.drawable.ic_moon)
    }

    binding.switchBtn.setOnClickListener(View.OnClickListener {
        if (isNightModeOn) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
            sharedPrefsEdit.putBoolean("NightMode", false)
            sharedPrefsEdit.apply()
            binding.switchBtn.setBackgroundResource(R.drawable.ic_moon)
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
            sharedPrefsEdit.putBoolean("NightMode", true)
            sharedPrefsEdit.apply()
            binding.switchBtn.setBackgroundResource(R.drawable.ic_sun)
        }
    })

}

private fun replaceFragment(fragment: Fragment) {
    if (fragment != null) {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.fragment_container, fragment)
        transaction.commit()
    }
}

这里有一些参考图片

device in light mode and app too

device In light mode and app in dark

device in dark mode and app in light (bad)

device in dark mode and app too

请帮忙

将其保存在您的两个主题文件中-

<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

这样设备设置就不会覆盖应用程序设置