android selectableItemBackground选择

Asi*_*iat 12 android android-selector

当状态被"激活"并且我想保留效果(波纹)时,我想改变我的视图的背景?attr:selectableItemBackground.是否可以扩展或组合选择器?attr:selectableItemBackground

Nit*_*Nit 11

您可以使用a LayerDrawable?attr:selectableItemBackground在您激活的状态颜色上绘制涟漪效果drawable().

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <selector>
            <item android:state_activated="true">
                <color android:color="?attr/colorPrimary"/>
            </item>
            <item>
                <color android:color="@android:color/transparent"/>
            </item>
        </selector>
    </item>
    <item android:drawable="?attr/selectableItemBackground"/>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

编辑: 因为在API 21之前不可能在XML drawable中使用主题属性,所以最好将涟漪效果绘制为前景可绘制,并将激活的颜色选择器绘制为可绘制的背景.

<View
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/yourView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:foreground="?attr/selectableItemBackground"
    android:background="@drawable/activated_color_selector">
Run Code Online (Sandbox Code Playgroud)

随着res/drawable/activated_color_selector.xml含:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true">
        <!-- Can't use the ?attr/colorPrimary before API 21 -->
        <color android:color="@color/primaryColor"/>
    </item>
    <item>
        <color android:color="@android:color/transparent"/>
    </item>
</selector>
Run Code Online (Sandbox Code Playgroud)


小智 0

要更改整个应用程序的波纹颜色,您可以将其添加到应用程序主题中

<item name="colorControlHighlight">@color/ripple</item>
Run Code Online (Sandbox Code Playgroud)