为什么我的动态主题不能正常显示?

Why is my dynamic theme not being displayed properly?

我有两个主题:默认主题(绿色)和红色变体。 我在 MainActivity 上动态设置了我的主题,但它看起来像是两个主题的奇怪组合。 由于某种原因,只有一些项目更改为新主题,而另一些则保持旧主题。 Image

红色和绿色部分都将色调设置为颜色重音:

HomeFragment.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/homeConstraint"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.HomeFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/homeToolsLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="horizontal">

            <ImageButton
                android:id="@+id/homeSortButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_round_sort_32"
                android:tint="?attr/colorAccent" />

            <ImageButton
                android:id="@+id/homeCancelButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_round_close_32"
                android:tint="?attr/colorAccent"
                android:visibility="gone" />

            <LinearLayout
                android:id="@+id/homeSelectionTools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="end"
                android:orientation="horizontal">

                <ImageButton
                    android:id="@+id/homeColorAll"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="15dp"
                    android:background="@android:color/transparent"
                    android:src="@drawable/ic_round_color_lens_32"
                    android:tint="?attr/colorAccent"
                    android:visibility="gone" />

                <ImageButton
                    android:id="@+id/homeTagAll"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="15dp"
                    android:background="@android:color/transparent"
                    android:src="@drawable/ic_round_label_32"
                    android:tint="?attr/colorAccent"
                    android:visibility="gone" />

                <ImageButton
                    android:id="@+id/homedeleteAll"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="15dp"
                    android:background="@android:color/transparent"
                    android:src="@drawable/ic_round_delete_forever_32"
                    android:tint="?attr/colorAccent"
                    android:visibility="gone" />

                <ImageButton
                    android:id="@+id/homeSelectButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="15dp"
                    android:background="@android:color/transparent"
                    android:src="@drawable/ic_round_done_32"
                    android:tint="?attr/colorAccent" />

            </LinearLayout>

        </LinearLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/homeRecycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/homeEmptyLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/homeEmptyIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="15dp"
            android:contentDescription="Tip"
            android:src="@drawable/ic_baseline_inbox_120"
            app:tint="?attr/colorAccent" />

        <TextView
            android:id="@+id/homeEmptyTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/nunito_sans_extrabold"
            android:text="What an empty place..."
            android:textAlignment="center"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/homeEmptyDesc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/nunito_sans_bold"
            android:text="There are no notes. Try creating a new one"
            android:textAlignment="center"
            android:textSize="20sp" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

首页Activity

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/homeConstraint"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.HomeFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/homeToolsLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="horizontal">

            <ImageButton
                android:id="@+id/homeSortButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_round_sort_32"
                android:tint="?attr/colorAccent" />

            <ImageButton
                android:id="@+id/homeCancelButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_round_close_32"
                android:tint="?attr/colorAccent"
                android:visibility="gone" />

            <LinearLayout
                android:id="@+id/homeSelectionTools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="end"
                android:orientation="horizontal">

                <ImageButton
                    android:id="@+id/homeColorAll"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="15dp"
                    android:background="@android:color/transparent"
                    android:src="@drawable/ic_round_color_lens_32"
                    android:tint="?attr/colorAccent"
                    android:visibility="gone" />

                <ImageButton
                    android:id="@+id/homeTagAll"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="15dp"
                    android:background="@android:color/transparent"
                    android:src="@drawable/ic_round_label_32"
                    android:tint="?attr/colorAccent"
                    android:visibility="gone" />

                <ImageButton
                    android:id="@+id/homedeleteAll"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="15dp"
                    android:background="@android:color/transparent"
                    android:src="@drawable/ic_round_delete_forever_32"
                    android:tint="?attr/colorAccent"
                    android:visibility="gone" />

                <ImageButton
                    android:id="@+id/homeSelectButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="15dp"
                    android:background="@android:color/transparent"
                    android:src="@drawable/ic_round_done_32"
                    android:tint="?attr/colorAccent" />

            </LinearLayout>

        </LinearLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/homeRecycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/homeEmptyLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/homeEmptyIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="15dp"
            android:contentDescription="Tip"
            android:src="@drawable/ic_baseline_inbox_120"
            app:tint="?attr/colorAccent" />

        <TextView
            android:id="@+id/homeEmptyTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/nunito_sans_extrabold"
            android:text="What an empty place..."
            android:textAlignment="center"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/homeEmptyDesc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/nunito_sans_bold"
            android:text="There are no notes. Try creating a new one"
            android:textAlignment="center"
            android:textSize="20sp" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

如您所见,HomeFragment 中的 homeSortButton 是红色的,MainActivity 中的 newFAB 是绿色的,但两者具有相同的值。

这是我的themes.xml文件:

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="android:windowBackground">@color/white</item>
        <item name="colorAccent">@color/green</item>
        <item name="colorPrimary">@color/green</item>
        <item name="colorPrimaryVariant">@color/green</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/green</item>
        <item name="colorSecondaryVariant">@color/green</item>
        <item name="colorOnSecondary">@color/white</item>
        <item name="android:overScrollMode">never</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">@android:color/transparent</item>
        <item name="android:navigationBarColor">@android:color/transparent</item>
        <!-- Customize your theme here. -->
    </style>
    
    <style name="Theme.MyApp.Red" parent="Theme.MyApp">
        <item name="android:windowBackground">@color/white</item>
        <item name="colorAccent">@color/red</item>
        <item name="colorPrimary">@color/red</item>
        <item name="colorPrimaryVariant">@color/red</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/red</item>
        <item name="colorSecondaryVariant">@color/red</item>
        <item name="colorOnSecondary">@color/white</item>
        <item name="android:overScrollMode">never</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">@android:color/transparent</item>
        <item name="android:navigationBarColor">@android:color/transparent</item>
    </style>

为了设置主题,我在我的设置片段中的 OnViewCreated() 中调用它:

appColorSpinner.onItemSelectedListener = object: AdapterView.OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {



                var themeColor: Int = 0

                when(position){
                    0 -> {
                        themeColor = GREEN_COLOR
                        mContext?.setTheme(R.style.Theme_MyApp)
                    }
                    1 -> {
                        themeColor = RED_COLOR
                        mContext?.setTheme(R.style.Theme_MyApp_Blue)
                    }
                }

                pref?.edit()?.putInt("colorTheme", themeColor)?.apply()
}

我在我的 MainActivity 的 OnCreate 中调用它来加载主题:

fun setCurrentTheme(){

        val prefs = getSharedPreferences("preferences", Context.MODE_PRIVATE)
        val color = prefs.getInt("colorTheme", GREEN_COLOR)

        when(color){
            GREEN_COLOR -> {
                setTheme(R.style.Theme_ZenNotes)
            }
            RED_COLOR -> {
                theme.applyStyle(R.style.Theme_ZenNotes_Red, true)
            }
            BLUE_COLOR -> {
                setTheme(R.style.Theme_ZenNotes_Blue)
            }
            YELLOW_COLOR -> {
                setTheme(R.style.Theme_ZenNotes_Yellow)
            }
            PURPLE_COLOR -> {
                setTheme(R.style.Theme_ZenNotes_Purple)
            }
        }
    }

是什么导致了这种奇怪的行为?

好吧,菜鸟犯错了。我不得不在我的 Activity.

上调用 setCurrentTheme() 之前调用 setContentView(...)