You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中Toolbar内ImageView设置selectableItemBackgroundBorderless失效怎么办

问题原因

Toolbar默认开启了子View裁剪,无边界水波纹超出ImageView边界的部分会被Toolbar直接裁剪,所以最终展示效果和带边界的水波纹一致。使用RelativeLayout时没有该裁剪限制,因此效果正常。
你之前将属性改为background时无效果,是因为ImageView的src绘制层级高于background,水波纹被图标挡住了,所以用foreground的配置方向是正确的。

解决方法

只需要给Toolbar添加两个关闭裁剪的属性即可:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/student_detail_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?android:actionBarSize"
    <!-- 新增以下两个属性 -->
    android:clipChildren="false"
    android:clipToPadding="false"
    app:layout_collapseMode="pin">

可选优化

如果部分低版本系统点击时水波纹不触发,可以给ImageView补充android:focusable="true"属性:

<ImageView
    android:id="@+id/student_detail_back_button_image_view"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_centerVertical="true"
    android:layout_marginEnd="32dp"
    android:clickable="true"
    android:focusable="true"
    android:foreground="?attr/selectableItemBackgroundBorderless"
    android:padding="3dp"
    android:src="@drawable/general_back_button"
    app:tint="@color/colorPrimaryIcon" />

如果主题适配问题导致水波纹还是有边界,可以将foreground属性替换为系统级属性:
android:foreground="?android:attr/selectableItemBackgroundBorderless"

内容的提问来源于stack exchange,提问作者zimmer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 04:48:01