首页 > 解决方案 > 可绘制文件在深色模式下不会改变颜色

问题描述

我目前正在处理一个 LoginActivity,我想制作一个圆形的线性来将 Edittext 放在里面,我使用一个 XML 文件来圆角它们,但唯一的问题是,它们在暗模式下不会改变颜色。

我已经使用了@colors文件中的颜色,但它根本不会影响,我尝试使用cardview而不是带有可绘制文件作为背景的Linearview,但它破坏了布局。

XML:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/design_default_color_on_primary"/>
    <stroke android:width="0dp" android:color="#B1BCBE" />
    <corners android:radius="20dp"/>
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>

登录活动.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=".LoginActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="center"
        android:src="@drawable/login_bg1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>


    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="@drawable/loginbg"
        android:elevation="25dp"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Login"
            android:layout_marginVertical="20dp"
            android:textStyle="bold"
            android:textSize="30sp"
            android:textColor="@color/design_default_color_primary"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="44dp"
            android:layout_marginStart="44dp"
            android:padding="10dp"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:background="@drawable/edittext_box"
            android:elevation="10dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_email"/>

            <EditText
                android:id="@+id/Email"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                android:background="@null"
                android:hint="Email"
                android:inputType="textEmailAddress"
                android:textColorHint="@color/design_default_color_primary"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="44dp"
            android:layout_marginStart="44dp"
            android:padding="10dp"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:background="@drawable/edittext_box"
            android:elevation="10dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_password"/>

            <EditText
                android:id="@+id/Password"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                android:background="@null"
                android:hint="Password"
                android:inputType="textPassword"
                android:textColorHint="@color/design_default_color_primary"/>
        </LinearLayout>

        <Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="Login"/>


    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

也许有人知道解决这个问题。

标签: androidxmlandroid-studioandroid-layoutandroid-dark-theme

解决方案


要支持暗模式,夜间版本需要单独的 colors.xml 文件。为夜间模式创建颜色文件。按着这些次序。右键单击 values 文件夹 New > Value Resource File 在 Available Qualifiers 中搜索 Night mode 并单击 (>>) 按钮。从下拉列表中选择 Night 将文件命名为颜色

并在此处定义所有夜间模式颜色

颜色的名称将与非夜间颜色文件相同

为夜间模式创建 colors.xml 的另一种方法是在左上角单击 Android 并切换到项目,转到文件夹 app > src > main > res 并创建一个新文件夹并将其命名为 values-night 并在该文件夹内创建一个名为 colors.xml 的 XML 文件


推荐阅读