首页 > 解决方案 > 具有类似 MaterialButton 的半透明波纹的 Android 自定义按钮

问题描述

我基于 MaterialButton 创建了自定义按钮,该按钮在按下和禁用时是半透明的(primaryColorDisabled)。现在,我想要让它的涟漪效应将按钮从它的primaryColor 变成primaryColorDisabled,而不是MaterialButton 的默认突出显示颜色。

我的颜色仅在 alpha 上有所不同,并且已定义:

<color name="primaryColor">#283593</color>
<color name="primaryColorDisabled">#A0283593</color>

对于我的自定义按钮,我应用以下样式:

<style name="CustomButton" parent="Widget.MaterialComponents.Button">
    <item name="android:background">@drawable/custom_background</item>
</style>

以下是我的可绘制对象的递归定义。我删除了不相关的部分,如命名空间、填充、边距等。

@drawable/custom_background:

<selector>
    <item android:state_enabled="false"
          android:drawable="@drawable/custom_disabled" />
    <item android:state_pressed="true"
          android:drawable="@drawable/custom_disabled" />
    <item android:drawable="@drawable/custom_ripple" />
</selector>

@drawable/custom_disabled:

<inset>
    <shape
        android:shape="rectangle"
        android:tint="@color/primaryColorDisabled" >
        <solid android:color="@color/primaryColor" />
    </shape>
</inset>

@drawable/custom_ripple:

<ripple android:color="?attr/colorControlHighlight">
    <item android:drawable="@drawable/custom_enabled" />
</ripple>

@drawable/custom_enabled:

<inset>
    <shape
        android:shape="rectangle" >
        <solid android:color="@color/primaryColor" />
    </shape>
</inset>

现在,我尝试在@drawable/custom_ripple中将颜色从?attr/colorControlHighlight 更改为我的@color/ primaryColorDisabled ,但是当我点击我的按钮时,没有任何变化:没有波纹,甚至没有“state_pressed”外观。任何想法如何实现这一目标?

标签: androidmaterialbutton

解决方案


推荐阅读