1

我正在尝试设置主题运行时间。并面临一个问题,我有一个布局,我进一步使用它来改变它的颜色取决于主题。但它显示随机颜色而不是我设置的颜色。

这是我尝试过的代码。

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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.DashboardActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/gradient_layout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.4"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10dp"
                    android:layout_marginTop="20dp"
                    android:layout_marginEnd="10dp"
                    android:gravity="center"
                    android:text="@string/app_name"
                    android:textColor="@android:color/background_light"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/company_name"
                    android:textColor="@android:color/background_light"
                    android:textSize="24sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1.6"
                android:background="@color/white_gray" />
        </LinearLayout>
    </FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

onCreate() 中的 Java 代码如下

preferences = getSharedPreferences("ThemePreference", MODE_PRIVATE);
        themeColor = preferences.getInt("primaryColor", R.color.colorPrimary);
        themeColorDark = preferences.getInt("darkColor", R.color.colorPrimary);

        if (themeColor == R.color.orange_colorPrimary) {
            setTheme(R.style.AppTheme_orange);
        } else if (themeColor == R.color.red_colorPrimary) {
            setTheme(R.style.AppTheme_red);
        } else if (themeColor == R.color.green_colorPrimary) {
            setTheme(R.style.AppTheme_green);
        } else if (themeColor == R.color.colorPrimary) {
            setTheme(R.style.AppTheme);
        }

gradientLayout = findViewById(R.id.gradient_layout);
gradientLayout.setBackgroundColor(themeColor);

我想在 gradientLayout 中设置颜色,我会在我的偏好中获得哪种颜色primaryColor

如果有人有解决方案,请回复。

先感谢您 !!

4

0 回答 0