保持笔画内的波纹

Keep ripple within stroke

我正在尝试为带有笔划的 Button 创建 ripple 背景 drawable

这是我目前所拥有的:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#336699">

    <item>
        <shape android:shape="rectangle">

            <solid android:color="#998811" />

            <stroke
                android:width="2dp"
                android:color="#119988" />

        </shape>
    </item>
</ripple>

但是使用这个解决方案,波纹与我的笔划重叠。
我只想要笔画中的ripple,我该怎么做?

根据 the documentation,您添加另一个 ID 为 @android:id/mask 的项目 - 这将限制涟漪的去向。您可以将其设置为插入,如下所示:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#336699">

    <item>
        <shape android:shape="rectangle">

            <solid android:color="#998811" />

            <stroke
                android:width="2dp"
                android:color="#119988" />

        </shape>
    </item>

    <item android:id="@android:id/mask">
        <inset android:insetBottom="2dp"
               android:insetLeft="2dp" 
               android:insetRight="2dp"
               android:insetTop="2dp">
            <shape android:shape="rectangle">
                <!-- Color doesn't matter -->
                <solid android:color="@android:color/white"/>
            </shape>
        </inset>
    </item>
</ripple>