为按钮背景颜色添加波纹效果到我的按钮? [英] 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" />
这篇关于为按钮背景颜色添加波纹效果到我的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!