【发布时间】:2017-02-21 20:18:34
【问题描述】:
我创建了一个按钮,我想为该按钮添加波纹效果!
我创建了一个按钮 bg 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 的不同博客中找到了其中一些代码,但仍然无法正常工作!
【问题讨论】:
-
下次请把截图缩小很多..(上传前调整大小)
-
@user25,您也可以在图片链接中添加
l或m。 (见我的编辑) -
那些想要转发
foreground属性的人,使用这个hack 来实现向后兼容性 - stackoverflow.com/a/65213848/4694013
标签: android android-layout android-drawable rippledrawable