我正在尝试在 android studio 中设计一个自定义按钮,但它没有被应用?

I am trying to design a custom button in android studio, but it isn't getting applied?

我之前也曾与 android 工作室合作过,并设计了自定义按钮并用于应用,但最近我遇到了颜色、形状和圆角半径未应用的问题 得到应用但颜色没有,它仍然是紫色。

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

    <corners android:radius="20dp"></corners>

    <solid android:color="#11fa30"></solid>
</shape>

我已将此文件保存在可绘制文件夹中。 我也把它应用到按钮的背景上

 <Button
                    android:id="@+id/stop_rec"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textStyle="bold"
                    android:layout_margin="1dp"
                    android:background="@drawable/button_pressed"
                    android:text="@string/stop_r" /> 

正在应用形状,但未应用颜色。有人可以指导我为什么会遇到这个问题。

将此行添加到按钮的 xml 代码中:

app:backgroundTint="@null"

所以前几天我只是试验了一些东西,我只能使用 androidx.appcompat.widget.AppCompatButton 而不是 normla 按钮来更改按钮背景和背景颜色。这样你就可以保持一切不变,只改变标签如下:

 <androidx.appcompat.widget.AppCompatButton
           android:id="@+id/stop_rec"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:textStyle="bold"
           android:layout_margin="1dp"
           android:background="@drawable/button_pressed"
           android:text="@string/stop_r" /> 

我建议您使用一种更易于重用的样式和一个可绘制的选择器来管理“按下按钮”状态;)

如果您也想将该形状用于其他视图,但使用另一种颜色,您可以在 android:background="@drawable/button_pressed" 下面添加背景色调。

我还注意到你有一个 layout_weight 没有将宽度或高度更改为 0。也许你忘了删除那个

希望我能帮上忙:)

使用 UTF-8 对文件进行编码,如果它不起作用,请更改 colors.xml

中的颜色值
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
 android:shape="rectangle">

<corners android:radius="20dp"></corners>

<solid android:color="#11fa30"></solid>
</shape>