Android中的材质圆形按钮?

Hen*_*ian 4 java android material-design

看看CyanogenMod音乐应用程序中的这个列表项:

在此输入图像描述

哪个View用于该设置按钮?它不可能是ImageButton因为当我按它时,会产生圆形波纹:

在此输入图像描述

也许一种方法是Toolbar在列表内部膨胀菜单按钮.但调用inflateMenu()需要最低API级别21.还有哪些其他方法可以实现此目的?谢谢!

Xav*_*ler 5

如果您想要圆形纹波,可以通过两种方式定义它.

  1. 您可以使用无界波纹 - 如果未定义形状或蒙版,则波纹默认为圆形
  2. 您可以将蒙版的形状专门定义为圆形

无限的波纹定义非常简单,如下所示:

<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@android:color/darker_gray" />
Run Code Online (Sandbox Code Playgroud)

如果您触摸View它将创建基本的圆形纹波,例如,当您输入PIN时,在默认的Android锁定屏幕上使用该纹理.


然而,根据定义,无界的涟漪没有任何定义的界限,因此并不总是你想要的.如果您只想专门定义纹波的形状和大小,可以使用蒙版.这具有以下优点:将不绘制用于定义形状的项目.它的工作原理如下:

<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@android:color/darker_gray">

    <!-- If you give an item this mask id then it will only be used to determine the shape of -->
    <!-- ripple, but will not be drawn in any way -->
    <item android:id="@android:id/mask">
        <!-- android:shape defines the shape of the ripple. I use oval for a circular one, -->
        <!-- but there are also rectangle, line, ring -->
        <shape android:shape="oval">
            <solid android:color="@android:color/white"/>
        </shape>
    </item>
</ripple>
Run Code Online (Sandbox Code Playgroud)

您可以从上面的两个选项中进行选择 - 这是您最喜欢的选项.只需将纹波指定为ImageView这样,它看起来应该与您问题中的图像非常相似:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ripple"
    android:src="@drawable/your_icon"
    android:clickable="true" />
Run Code Online (Sandbox Code Playgroud)

我添加了clickable上面的属性,因此即使您没有为其分配单击侦听器,也会启用波纹ImageView.只要你指定一个点击监听器,你就不需要它.