在CyanogenMod音乐应用程序中查看此列表项:
该设置按钮使用了哪个View
?它不可能是ImageButton
因为当我按下它时会产生圆形波纹:
也许一种方法是在列表中的Toolbar
中夸大菜单按钮。但是调用inflateMenu()
需要最低API级别21.还有哪些其他方法可以实现此目的?谢谢!
答案 0 :(得分:5)
如果您想要圆形纹波,可以通过两种方式定义它。
无限的波纹定义非常简单:
<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
分配点击监听器,也会启用涟漪效果。只要你指定一个点击监听器,你就不需要它。