按钮显示的颜色不正确-Android Studio

问题描述

在我的Android Studio应用程序中,我有一个按钮,将其设置为可绘制背景。可绘制对象圆角化并更改按钮的颜色。 但是,当我查看活动的最终设计时,我想要的颜色没有显示出来。而是,该按钮采用colorPrimary(橙色,#ED3616)。当我运行应用程序时,也会发生同样的情况。 注意:可绘制文件的其他属性可以完美地转换为按钮(即圆角)。问题只在于颜色。

我尝试使用MaterialButton,并使用android:backgroundTint设置颜色。然后,颜色确实出现了,但是设计看起来很奇怪(按钮(Picture of MaterialButton if you'd like to see it里面有一个不同的彩色矩形)

如何使常规按钮具有所需的颜色?

按钮:

<Button
    android:background="@drawable/round2"
    android:id="@+id/signIn"
    android:layout_width="256dp"
    android:layout_height="66dp"
    android:text="@string/logIn"
    android:textColor="#FFFFFF"
    android:textSize="25sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.496"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.878" />

可绘制:round2.xml:

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
        android:color="#B30C44"
        android:paddingLeft="6dp"
        android:paddingTop="6dp" />
    <corners
        android:bottomrighTradius="20dp"
        android:bottomLefTradius="20dp"
        android:topLefTradius="20dp"
        android:topRighTradius="20dp"/>
    <padding
        android:bottom="6dp"
        android:left="6dp"
        android:right="6dp"
        android:top="6dp" />
</shape>

显示内容What the Button looks like

解决方法

之所以会这样,是因为MaterialButton的默认样式(如果您使用的是Button主题,则MaterialButton会自动替换为MaterialComponentstints the background colorPrimary

您必须使用:

<Button
    android:background="@drawable/round2"
    app:backgroundTint="@null"
    ... />