使用 Theme.MaterialComponents.DayNight.NoActionBar 作为主题时无法自定义按钮

Cannot customize button when using Theme.MaterialComponents.DayNight.NoActionBar as theme

我想使用自定义可绘制文件自定义一个按钮,如下所示:

custom_button.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#48cae4" />
    <corners android:radius="10dp" />
</shape>

但是改变按钮的背景后(如下)没有效果

android:background="@drawable/custom_button" 

但是,如果我将<style name="Theme.A" parent="Theme.MaterialComponents.DayNight.NoActionBar">更改为<style name="Theme.A" parent="Theme.AppCompat.DayNight.NoActionBar">,那么自定义drawble将在按钮上生效,但随后我使用的Material Components将转换为普通小部件。

您不能为 Material 按钮或使用 Material 主题使用自定义可绘制对象。您只能设置纯色状态并使用 app:backgroundTint 属性设置它们。 Ref

res/color/ 目录中创建 tint.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="?attr/colorPrimary" android:state_pressed="true"/>
    <item android:color="@color/colorSecondary" />
    ...
</selector>

在您的按钮或按钮样式中使用它:

app:backgroundTint="@color/tint"