安卓ImageButton水波纹效果

5

好的,我了解水波纹效果只在LOLLIPOP及以上版本可用。但是,当我设置我的ImageButton时,仍然无法获得像“常规”按钮一样工作的漂亮水波纹效果,只显示一个图像(和透明背景)...

我添加了AppCompat v7,并将第二个布局放在我的drawable/layout-v21文件夹中,其中包含以下按钮:

<ImageButton
    android:id="@+id/forward"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:width="15dp"
    android:scaleType="fitCenter"
    android:height="15dp"
    android:padding="25dp"
    style="@style/Widget.AppCompat.ImageButton"
    android:src="@drawable/forwardplay" />

但是背景是灰色的,水波纹(也是灰色的)无法自定义。
因此,如果我只有一个漂亮的水波纹可供放置在我的drawable-v21中,并且我可以从ImageButton的background属性中引用它,那将是很棒的事情。问题是我找不到原始的Android Ripple效果(我有一部三星S6手机,这是三星的主题吗?)
希望有人能分享他们的工作drawable。
谢谢!

试试这个:android:background="?attr/selectableItemBackgroundBorderless" - Satan Pandeya
不要。会导致应用崩溃。编译为SDK 23(已经得到了v25 - 两者都试过...)。 - rubmz
6个回答

17

试试这个,肯定会起作用:

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/YOUR_BUTTON_SOURCE"
    android:background="?attr/selectableItemBackgroundBorderless"
/>

1
如果您已经有不同的背景,那么android:foreground="?attr/selectableItemBackgroundBorderless"同样适用。 - Panos Gr
3
android:foreground= 只适用于API 23及以上版本。如果你想要将背景设置为透明,只需使用android:background="?attr/selectableItemBackgroundBorderless",它会将背景设置为透明并且具有漂亮的涟漪效果。 - Husnain Qasim

2
您可以制作可绘制对象并将其设置为按钮的背景。
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/grey_15">
    <item android:id="@android:id/mask">
        <shape android:shape="oval">
            <solid android:color="?android:colorPrimary"/>
        </shape>
        <color android:color="@color/white"/>
    </item>
</ripple>

更改您规格的颜色。


不行。我试过了,它看起来像一个不会变大的小点。 - rubmz

2
尝试以下答案后:
<?xml version="1.0" encoding="utf-8"?>
<ripple android:color="#ffffff"
        xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/mask"
          android:drawable="@android:color/white" />
</ripple>

我发现了一种心灵的平静 :) 功能非常好!

总结:

  1. put your layout in layout-v21 to support newer device (and keep a copy for older devices in the regular drawable/layout folder.
  2. create a drawable-v21 folder and put the text above in a file named ripple.xml
  3. set it as background like so:

    <ImageButton
        android:id="@+id/forward"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="15dp"
        android:scaleType="fitCenter"
        android:height="15dp"
        android:padding="25dp"
        android:background="@drawable/ripple_bg"
        android:src="@drawable/forwardplay" />
    

2

…这是三星的主题吗?

很有可能是,但无论如何,以下方法可以实现这种效果。

这取决于你想要达到的外观。如果你使用的图标类似于操作栏中看到的内容,你可能需要使用actionButtonStyle

style="?attr/actionButtonStyle"

还有一些其他的按钮样式属性可供选择,可能包括涟漪效果,也可能不包括。

否则,如果您使用实际图像作为按钮,可以使用selectableItemBackgroundselectableItemBackgroundBorderless属性作为前景:

android:foreground="?attr/selectableItemBackground"

0

尝试使用

android:background="?selectableItemBackground"

不,灰色背景看起来有点糟糕。 - rubmz

0
尝试使用


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接