我创建了一个按钮,并想为该按钮添加水波纹效果!
我创建了一个按钮背景XML文件:(bg_btn.xml)
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#FFFFFF" android:endColor="#00FF00" android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#000000" />
</shape>
这是我的涟漪效果文件:(ripple_bg.xml)
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#f816a463"
tools:targetApi="lollipop">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#f816a463" />
</shape>
</item>
</ripple>
这是我想要添加涟漪效果的按钮:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="173dp"
android:textColor="#fff"
android:background="@drawable/ripple_bg"
android:clickable="true" />
但是在添加涟漪效果后,按钮的背景变成透明的,并且只有在点击时才显示按钮,就像这样:
点击之前
点击之后
但我需要按钮的背景颜色和涟漪效果,我在 Stack Overflow 的不同博客中找到了一些代码,但仍然无法正常工作!
foreground
属性的人,可以使用这个向后兼容的 hack - https://dev59.com/CVkS5IYBdhLWcg3wV1Zy#65213848 - Anoop M Maddasseri