自动关闭设备上的黑暗模式,(或其他解决方案)Android StudioKotlin

bq9c1y66  于 11个月前  发布在  Kotlin
关注(0)|答案(2)|浏览(126)

所以我需要一点帮助。
我的问题是,当我在我的应用程序中有一个功能用于更改为黑暗模式时,当我在设备处于明亮模式时更改它时,它会改变得很好,但是当我在设备处于黑暗模式时更改它时,它不会改变一些东西(背景和文本)。
我已经找到了一个解决方案,当我在设备选项中禁用黑暗模式,它工作得很好.我只是想改变应用程序的主题时,你在应用程序中更改它,而不是当设备主题被更改,因为它会破坏它.这个问题不是发生在像素4模拟器,只是在我的POCO F2 Pro MIUI 12.5.5手机.
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>


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

请帮助

yi0zb3m4

yi0zb3m41#

把这个放在你的两个主题文件中-

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

字符串
以便设备设置不能覆盖应用程序设置

dpiehjr4

dpiehjr42#

只是一个简单的解决方案从项目中删除您的夜间/主题文件,这样它就不会允许应用程序在黑暗的主题中打开。

相关问题