【问题标题】:Implement ripple effects on imageButton在 imageButton 上实现波纹效果
【发布时间】:2017-10-01 13:17:22
【问题描述】:

我正在尝试在 Imagebutton 中实现波纹效果。

activty_main.xml

        <ImageButton
        android:id="@+id/idbuttonsix"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/iconagift"
        android:background="@drawable/ripple_effect"

我已尝试实现此代码,但触摸时 ImageButton 上没有涟漪效果:

android:foreground="?android:attr/selectableItemBackground"

还有这个

android:background="?attr/selectableItemBackgroundBorderless"

我正在寻找可以一步一步帮助我的人。

ripple_effect.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorAccent">
<item>
    <shape
        android:shape="rectangle">
        <solid android:color="#4CAF50" />
    </shape>
</item>

【问题讨论】:

  • 您在哪个 Android 版本上运行应用程序?还发布您的ripple_effect.xml
  • android:clickable="true" 试试这个
  • @NabinBhandari 这个应用程序 - minSdkVersion 21 和 targetSdkVersion 26

标签: android imagebutton


【解决方案1】:

<ImageButton
    android:id="@+id/idbuttonsix"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"/>

请注意,我在这里删除了“src”标签,并且刚刚使用了“background”属性。这肯定会为您提供此按钮的涟漪效果。

【讨论】:

    【解决方案2】:

    尝试从图像视图中删除 src 标签,并尝试添加此背景(而不是前景):

    android:background="?android:attr/selectableItemBackground"

    【讨论】:

    • 谢谢你,@Rabillo,但是怎么做...?我需要这个图标“@drawable/iconagift”
    • 如何将按钮包含在框架布局中:将框架布局的背景设置为可绘制对象,将按钮的背景设置为 android:background="?android:attr/selectableItemBackground"跨度>
    【解决方案3】:

    要使用这个库(android 9+):RippleEffect

    集成

    dependencies {
        compile 'com.github.traex.rippleeffect:library:1.3'
    }
    

    用法:

    <com.andexert.library.RippleView
      android:id="@+id/more"
      android:layout_width="?android:actionBarSize"
      android:layout_height="?android:actionBarSize"
      android:layout_toLeftOf="@+id/more2"
      android:layout_margin="5dp"
      rv_centered="true">
    
      <ImageView
        android:layout_width="?android:actionBarSize"
        android:layout_height="?android:actionBarSize"
        android:src="@android:drawable/ic_menu_edit"
        android:layout_centerInParent="true"
        android:padding="10dp"
        android:background="@android:color/holo_blue_dark"/>
    
    </com.andexert.library.RippleView>
    

    希望对你有帮助。

    【讨论】:

    • 在您的 App Gridle 文件中添加您的依赖项。
    • 是的,它可以工作,但是当我关闭活动时,它会显示更大的圆圈
    • 它们太大了
    猜你喜欢
    • 2015-02-08
    • 2017-05-01
    • 2023-03-31
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多