背景颜色设置不正确。它显示的颜色与设置的颜色不同

时间:2020-10-29 08:10:57

标签: android background-color android-drawable app-themes

我正在尝试设置主题运行时间。并面临一个问题,我有一种布局,我将根据主题进一步更改其颜色。但是它显示的是随机颜色,而不是我设置的颜色。

这是我尝试过的代码。

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

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

提前谢谢!

0 个答案:

没有答案