Android 13及以上selectableItemBackgroundBorderless涟漪效果失效求助
解决Android 13+下
selectableItemBackgroundBorderless涟漪失效问题 问题分析
Android 13(API 33)及以上系统中,直接设置android:background="?android:attr/selectableItemBackgroundBorderless"无法触发超出View边界的圆形涟漪,但selectableItemBackground(带边界的矩形涟漪)可正常生效,低版本无此问题。这是因为Android 13对系统触摸反馈Drawable的应用逻辑做了调整,borderless属性的生效条件更严格。
可行解决方案
1. 自定义Borderless涟漪Drawable
直接创建自定义涟漪效果,完全模拟selectableItemBackgroundBorderless的行为,不受系统版本限制:
在res/drawable/下创建ripple_borderless.xml:
<?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:attr/colorControlHighlight"> <item android:id="@android:id/mask"> <shape android:shape="oval"> <solid android:color="@android:color/white" /> </shape> </item> </ripple>
给目标View设置背景:
<View android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/ripple_borderless" android:clickable="true" android:focusable="true" />
2. 改用android:foreground属性
在Android 13+中,将borderless属性设置到foreground而非background,可恢复正常效果:
<View android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/transparent" android:foreground="?android:attr/selectableItemBackgroundBorderless" android:clickable="true" android:focusable="true" />
3. 检查View基础配置
确保目标View已开启核心属性,这是涟漪效果生效的前提:
- 必须添加
android:clickable="true" - 可选添加
android:focusable="true"(适配键盘导航场景)
内容的提问来源于stack exchange,提问作者Rahul Mishra
相关产品推荐
相关产品推荐

