如何添加涟漪效应,并有自定义背景上的按钮在android



请告诉我如何同时实现这两者,我已经经历了很多问题,但他们没有具体处理我的问题。

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

我尝试了下面列出的一些事情。

第一种方法:我尝试在<Button>标签中设置style="@style/AppTheme"。这给了我涟漪效果和背景颜色(在样式中使用<item name="colorButtonNormal">#500347</item>),但按钮不是圆润的。

因此,为了实现圆形,我在<Button>标签中添加了android:background="@drawable/button_bg"。这使得按钮圆润,也实现了背景色,但涟漪效应消失了。

第二个方法:我尝试使用选择器。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>
  • button in 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" />
    

  • 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>
    

  • <Button>标签android:background="@drawable/button_bg"
  • 使用此方法,按钮按下后变为白色,返回背景色。

    请告诉我怎么做。

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

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

    我是这样做的:-

    1. android:background="@drawable/ad_action_view_bg"在我看来。//背景是一个Ripple绘制
    2. 然后我将android - foreground设置为所需的颜色。

    最新更新