如何添加涟漪效果并在Android中的按钮上有自定义背景?

时间:2023-01-19 16:40:10

Please tell me how to achieve both these together, I have gone through many already questions, but they dont specifically deal with my issue.

请告诉我如何实现这两个目标,我已经经历了很多已经存在的问题,但他们并没有专门处理我的问题。

I want to have ripple effect on my button as well as it should be rounded and have background color. Also I should be able to control ripple effect color.

我想在我的按钮上产生涟漪效果,并且它应该是圆形的并且具有背景颜色。此外,我应该能够控制涟漪效果的颜色。

I tried few things which are listed below.

我尝试了下面列出的一些东西。

First Method: I tried setting style="@style/AppTheme"in the <Button> tag. This gives me ripple effect and background color (using <item name="colorButtonNormal">#500347</item>in the style) but the button is not rounded.

第一种方法:我尝试在

So to achieve rounded shape I gave android:background="@drawable/button_bg" in the <Button> tag. This makes button rounded and also background color is achieved, But Ripple effect is gone.

所以为了实现圆形,我在

Second Method: I tried the using selector. 1. Created button_background.xml

第二种方法:我尝试了使用选择器。 1.创建了button_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:state_focused="true"
        android:drawable="@drawable/button_bg"/>

    <item
        android:state_pressed="true"
        android:drawable="@drawable/ripple_effect"/>

    <item 
        android:drawable="@drawable/ripple_effect"/>

</selector>
  1. rounded background for button in button_bg.xml:

    button_bg.xml中按钮的圆形背景:

      <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="#500347" />
    
    <corners
        android:radius="10dip" />
    
    <padding
        android:bottom="0dip"
        android:left="0dip"
        android:right="0dip"
        android:top="0dip" />
    

  2. ripple_effet.xml:

        <ripple xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
      android:color="@android:color/holo_green_dark"
       tools:targetApi="lollipop">
    
    
        <item android:id="@android:id/mask">
    
    <shape android:shape="rectangle">
    
        <solid android:color="@android:color/holo_green_dark" />
    
        <corners android:radius="10dip" />
    
    </shape>
    

  3. ripple_effet.xml:               

  1. In <Button> tag android:background="@drawable/button_bg"

With this method button just goes white when pressed returns to background color.

使用此方法按钮在按下时会变为白色,返回背景颜色。

Please tell me how to do it.

请告诉我怎么做。

2 个解决方案

#1


1  

Found my answer here. https://github.com/traex/RippleEffect/blob/master/README.md

在这里找到我的答案。 https://github.com/traex/RippleEffect/blob/master/README.md

But if there is any other easier way, more answers are welcome

但如果还有其他更简单的方法,欢迎提出更多答案

#2


0  

I did it this way :-

我是这样做的: -

  1. android:background="@drawable/ad_action_view_bg" to my view. // Background is a Ripple drawable
  2. 我认为android:background =“@ drawable / ad_action_view_bg”。 //背景是Ripple drawable

  3. Then I set android - foreground to required color.
  4. 然后我将android - foreground设置为所需的颜色。

#1


1  

Found my answer here. https://github.com/traex/RippleEffect/blob/master/README.md

在这里找到我的答案。 https://github.com/traex/RippleEffect/blob/master/README.md

But if there is any other easier way, more answers are welcome

但如果还有其他更简单的方法,欢迎提出更多答案

#2


0  

I did it this way :-

我是这样做的: -

  1. android:background="@drawable/ad_action_view_bg" to my view. // Background is a Ripple drawable
  2. 我认为android:background =“@ drawable / ad_action_view_bg”。 //背景是Ripple drawable

  3. Then I set android - foreground to required color.
  4. 然后我将android - foreground设置为所需的颜色。