Android中的材质圆形按钮?

时间:2015-12-07 11:00:27

标签: java android material-design

在CyanogenMod音乐应用程序中查看此列表项:

enter image description here

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

enter image description here

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

1 个答案:

答案 0 :(得分:5)

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

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

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

    如果触摸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>
    

    您可以从上面的两个选项中进行选择 - 这是您最喜欢的选项。只需将纹波指定为ImageView,就像你的问题中的图像一样:

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ripple"
        android:src="@drawable/your_icon"
        android:clickable="true" />
    

    我在上面添加了clickable属性,因此即使您没有为ImageView分配点击监听器,也会启用涟漪效果。只要你指定一个点击监听器,你就不需要它。