xml 中的 Ripple drawable 不工作

Ripple drawable in xml not working

我遇到了一个问题:我有一个 xml 可绘制对象,我想用作单选按钮的背景,但涟漪效果不起作用。有人可以帮我吗?

我的xml背景:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#8bc34a" >
<item>
   <selector>
   <item android:drawable="@drawable/tb_checked" android:state_checked="true"></item>
<item android:drawable="@drawable/transparent"></item>
</selector>
</item>
</ripple>

如果您想在附加内容上方或下方显示无限制的波纹,请使用 <layer-list> 容器堆叠它们并且不要在 <ripple> 元素内放置任何内容。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <ripple android:color="#8bc34a" />
    </item>
    <item>
        <selector>
            <item android:drawable="@drawable/tb_checked"
                  android:state_checked="true" />
            <item android:drawable="@drawable/transparent" />
        </selector>
    </item>
</layer-list>

在 RadioButton xml 声明中添加此属性 android:background="?android:selectableItemBackground" 我很确定这会奏效。

<RadioButton
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:padding="@dimen/activity_horizontal_margin"
    android:text="Choice 1"
    android:gravity="center"
    android:background="@drawable/button_choice_white"
    android:button="@null"/>

在 v1-drawable 中添加带波纹的背景 drawable

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
<item android:id="@android:id/mask">
    <shape android:shape="rectangle">
        <solid android:color="?attr/colorAccent" />
    </shape>
</item>
<item>
    <selector>
        <item android:state_pressed="true">
            <shape android:shape="rectangle">
                <solid android:color="?attr/colorAccent"/>
            </shape>
        </item>

        <item android:state_checked="true">
            <shape android:shape="rectangle">
                <solid android:color="?attr/colorAccent"/>
            </shape>
        </item>

        <item>
            <shape android:shape="rectangle">
                <solid android:color="@color/button_choice_background"/>
            </shape>
        </item>
    </selector>
</item>