Change ?selectableItemBackgroundBorderless ripple color

无人久伴 提交于 2021-02-07 12:14:42

问题


I'm trying to use ?selectableItemBackgroundBorderless to create borderless ripple for a LinearLayout, it works fine but is not clearly visible.

How can I change the default color for ?selectableItemBackgroundBorderless to make the ripple effect visible?

I've tried applying ThemeOverlay.AppCompat.Dark theme to the parent layout but it doesn't help.


回答1:


As mentioned in the Theming with AppCompat blog post:

colorControlHighlight controls the ripple coloring

So creating a ThemeOverlay.AppCompat that sets that value to your chosen color will allow you to change the ripple color for that view and its children.




回答2:


An alternative would be to make your own selector. I know it's not the same nice solution, but it could be a solution anyway:

res/drawable/circular_item_background_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <shape android:shape="oval">
            <solid android:color="@color/your_color"/>
        </shape>
    </item>

    <item android:drawable="@android:color/transparent"/>

</selector>

res/drawable-v21/circular_item_background_selector.xml

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

    <item android:drawable="@android:color/transparent"/>

    <item android:id="@android:id/mask">
        <shape android:shape="oval">
            <solid android:color="@color/your_color"/>
        </shape>
    </item>

</ripple>



回答3:


Using the attribute foreground instead of background works well for me:

<Button
...    
android:foreground="?android:attr/selectableItemBackground"
android:backgroundTint="@color/blue"
android:textColor="@color/white"
.../>


来源:https://stackoverflow.com/questions/41496624/change-selectableitembackgroundborderless-ripple-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!