Android: can you have a ripple effect whose default state color is transparent?

两盒软妹~` 提交于 2019-12-18 12:47:53

问题


Something like the following, but it doesn't work. If I switch the drawable color to something like blue, it works.

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item android:drawable="@android:color/transparent"/>
</ripple>

回答1:


It's necessary to add a mask:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
     <item android:id="@android:id/mask">
       <color android:color="@android:color/white" />
     </item>
</ripple>



回答2:


Somehow, the @JMPergar 's answer didn't work for me.

However, I was able to think of this workaround: if a colour behind your transparent button is solid (not a gradient or a speckled picture) - you can use that color as a main button's unpressed one.

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="@color/profile_transparent_button_pressed"
        >

    <item>
        <shape>
            <corners android:radius="@dimen/profile_transparent_button_corner_radius" />
            <solid android:color="@color/profile_background" />
        </shape>
    </item>
</ripple>

where @color/profile_transparent_button_pressed is a colour to highlight a button, and @color/profile_background - the colour of a layout behind this button. It works exactly as expected, but still definitely is a workaround so you go try @JMPergar 's answer first.



来源:https://stackoverflow.com/questions/28308708/android-can-you-have-a-ripple-effect-whose-default-state-color-is-transparent

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