看看 CyanogenMod 音乐应用程序中的这个列表项:
哪个 View
用于该设置按钮?它不可能是 ImageButton
,因为当我按下它时,它会产生圆形波纹:
也许一种方法是在列表内的 Toolbar
中添加一个菜单按钮。但是调用 inflateMenu()
需要最低 API 级别 21。还有什么其他方法可以实现这一点?谢谢!
最佳答案
如果你想要圆形波纹,你可以用两种方式定义它。
- 您可以使用无界波纹 - 如果您没有定义形状或 mask ,波纹默认为圆形
- 你可以具体定义mask的形状为圆形
无界波纹的定义非常简单,如下所示:
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@android:color/darker_gray" />
如果您触摸View
,它会产生基本的圆形波纹,例如,当您输入 PIN 码时,它会在默认的 Android 锁定屏幕上使用。
然而,根据定义,无界涟漪没有任何定义的边界,因此并不总是您想要的。如果您只想专门定义波纹的形状和大小,您可以使用 mask 。这样做的好处是不会绘制用于定义形状的项目。它是这样工作的:
<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
分配点击监听器,也会启用波纹。只要您分配一个点击监听器,您就真的不需要它。
关于java - Android中的 Material 圆形按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34131962/