Android:selectableItemBackground没有处理带有xml drawable的按钮

时间:2017-07-23 20:55:01

标签: android xml

我制作了一个带有xml drawable的圆形按钮,并应用android:foreground="?attr/selectableItemBackground"以获得涟漪效果。但是,涟漪效应不起作用。反正是否有涟漪效应?作为一种解决方法,我使用FrameLayout包裹了按钮并对其应用了android:foreground="?attr/selectableItemBackground",但在这种情况下,纹波是矩形的并且超出了按钮(因为它是圆形的)。

的xml:

<Button
            android:id="@+id/reg_next"
            android:layout_width="125dp"
            android:layout_height="40dp"
            android:text="@string/next"
            android:layout_gravity="center"
            android:textAllCaps="false"
            android:textColor="@color/colorPrimary"
            android:textSize="18sp"
            android:background="@drawable/rounded_button_background"
            android:foreground="?attr/selectableItemBackground"/>

rounded_button_background:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" android:padding="10dp">
    <solid android:color="#ebeff1"/> <!-- this one is ths color of the Rounded Button -->
    <stroke android:width="1px" android:color="#979797" />
    <corners
        android:radius="20dp"
        android:bottomLeftRadius="20dp"
        android:topLeftRadius="20dp"
        android:topRightRadius="20dp"/>
    </shape>

1 个答案:

答案 0 :(得分:3)

您需要创建ripple drawable并将其设置为按钮背景(不含前景属性):

rounded_button_background.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#2793c9">
    <item>
        <shape
            android:padding="10dp"
            android:shape="rectangle">
            <solid android:color="#ebeff1" /> <!-- this one is ths color of the Rounded Button -->
            <stroke
                android:width="1px"
                android:color="#979797" />
            <corners
                android:bottomLeftRadius="20dp"
                android:radius="20dp"
                android:topLeftRadius="20dp"
                android:topRightRadius="20dp" />
        </shape>
    </item>
</ripple>

如果您需要支持api bellow 21(没有涟漪效应),请将此drawable添加到目录 drawable-v21 ,并在目录 drawable 中创建这些文件:

rounded_btn_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle">
    <solid android:color="#ebeff1" /> <!-- this one is ths color of the Rounded Button -->
    <stroke
        android:width="1px"
        android:color="#979797" />
    <corners
        android:bottomLeftRadius="20dp"
        android:radius="20dp"
        android:topLeftRadius="20dp"
        android:topRightRadius="20dp" />
</shape>

rounded_btn_active.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle">
    <solid android:color="#2793c9" /> <!-- this one is ths color of the Rounded Button -->
    <stroke
        android:width="1px"
        android:color="#979797" />
    <corners
        android:bottomLeftRadius="20dp"
        android:radius="20dp"
        android:topLeftRadius="20dp"
        android:topRightRadius="20dp" />
</shape>

rounded_button_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/rounded_btn_active" android:state_enabled="true" android:state_pressed="true" />
    <item android:drawable="@drawable/rounded_btn_normal" android:state_enabled="true" />
</selector>