0

当我将应用程序切换到暗模式而设备处于亮模式或相反时,片段会闪烁,所以如果我在设备处于亮模式时将应用程序切换到暗模式,一旦我打开包含 BottomNavigationView(5 个片段)的 MainActivity,第一个片段显示在亮模式闪烁一秒钟,然后转换为暗模式。

注意:我没有 AppCompatDelegate.setDefaultNightMode(pref.getStyleMode())在任何片段中添加切换暗模式代码,我已经尝试添加它,但仍然是同样的问题,我尝试了其他一些解决方案,但同样的事情仍然发生!

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        app:labelVisibilityMode="labeled"
        app:itemIconTint="@drawable/bottom_nav_icon_color_selector"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:itemTextColor="@color/navigation_text_color"
        app:menu="@menu/bottom_nav_menu" />

    <FrameLayout
        android:id="@+id/main_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/navView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_height="wrap_content"
        android:visibility="gone"/>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

     override fun onCreate(savedInstanceState: Bundle?) {

        AppCompatDelegate.setDefaultNightMode(pref.getStyleMode())

        super.onCreate(savedInstanceState)

        setContentView(getLayoutRes().layout)

        bindView(savedInstanceState)

    }

外观活动.kt

       override fun onClick(v: View?) {
        when (v?.id) {
            consSystem.id -> {
                if (pref.getStyleMode() == MODE_NIGHT_FOLLOW_SYSTEM) return
                pref.setStyleMode(MODE_NIGHT_FOLLOW_SYSTEM)
                updateUi(MODE_NIGHT_FOLLOW_SYSTEM)
                setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM)
//                delegate.localNightMode = MODE_NIGHT_FOLLOW_SYSTEM
            }
            consLight.id -> {
                if (pref.getStyleMode() == MODE_NIGHT_NO) return
                pref.setStyleMode(MODE_NIGHT_NO)
                updateUi(MODE_NIGHT_NO)
                setDefaultNightMode(MODE_NIGHT_NO)
//                delegate.localNightMode = MODE_NIGHT_NO
            }
            consDark.id -> {
                if (pref.getStyleMode() == MODE_NIGHT_YES) return
                pref.setStyleMode(MODE_NIGHT_YES)
                updateUi(MODE_NIGHT_YES)
                setDefaultNightMode(MODE_NIGHT_YES)
//                delegate.localNightMode = MODE_NIGHT_YES
            }
        }
    }
4

0 回答 0