使用按钮背景颜色为我的按钮添加波纹效果? [英] Add ripple effect to my button with button background color?
问题描述
我创建了一个按钮,我想为该按钮添加涟漪效果!
I created a button and I want to add ripple effect to that button!
我创建了一个按钮 bg XML 文件:(bg_btn.xml)
I created a button bg XML file: (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)
And this is my ripple effect file: (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>
这是我要添加涟漪效果的按钮:
And This is my Button which I want to add ripple effect:
<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" />
但是添加涟漪效果后按钮背景是透明的,并且按钮只有在点击时才显示,像这样:
But after adding ripple effect button background is transparent, and button display only when clicked, like this:
点击前
点击
但我需要按钮背景颜色和波纹效果,我在 Stack Overflow 的不同博客中找到了一些这样的代码,但仍然无法正常工作!
But I need both button background color and ripple effect, I found some of this code in different blogs of Stack Overflow, but still it is not working!
推荐答案
这是另一个可绘制的 xml,适用于那些想要将渐变背景、圆角半径和波纹效果添加在一起的人:
Here is another drawable xml for those who want to add all together gradient background, corner radius and ripple effect:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorPrimaryDark">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="@color/colorPrimaryDark" />
<corners android:radius="@dimen/button_radius_large" />
</shape>
</item>
<item android:id="@android:id/background">
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="@color/colorPrimaryLight"
android:startColor="@color/colorPrimary"
android:type="linear" />
<corners android:radius="@dimen/button_radius_large" />
</shape>
</item>
</ripple>
将此添加到按钮的背景中.
Add this to the background of your button.
<Button
...
android:background="@drawable/button_background" />
PS:此答案适用于 android api 21 及更高版本.
PS: this answer works for android api 21 and above.
这篇关于使用按钮背景颜色为我的按钮添加波纹效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!