且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用按钮背景颜色为我的按钮添加波纹效果?

更新时间:2023-01-26 23:30:15

这是另一个可绘制的 xml,适用于那些想要将渐变背景、圆角半径和波纹效果添加在一起的人:

Here is another drawable xml for those who want to add all together gradient background, corner radius and ripple effect:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/colorPrimaryDark">
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="@color/colorPrimaryDark" />
            <corners android:radius="@dimen/button_radius_large" />
        </shape>
    </item>

    <item android:id="@android:id/background">
        <shape android:shape="rectangle">
            <gradient
                android:angle="90"
                android:endColor="@color/colorPrimaryLight"
                android:startColor="@color/colorPrimary"
                android:type="linear" />
            <corners android:radius="@dimen/button_radius_large" />
        </shape>
    </item>
</ripple>

将此添加到按钮的背景中.

Add this to the background of your button.

<Button
    ...
    android:background="@drawable/button_background" />

PS:此答案适用于 android api 21 及更高版本.

PS: this answer works for android api 21 and above.