Android Studio中Floating Action Button无法显示源图片该如何解决?
问题现象
开发过程中为Floating Action Button设置的源图片无法正常渲染,仅显示纯色块或系统默认占位图形,异常效果示例如下:
修复方案
- 校验Material组件依赖
打开项目级或模块级build.gradle文件,确认已引入Material Design官方组件依赖,示例配置如下(可替换为最新稳定版本):implementation 'com.google.android.material:material:1.11.0' - 修正FAB属性配置
- 不要使用
android:src设置图标,改用兼容属性app:srcCompat绑定图标资源 - 若不需要对图标进行染色处理,将
app:tint属性设置为@null,避免系统默认的染色效果覆盖原图样式
正确配置示例:
<com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/你的图标资源名" app:tint="@null" /> - 不要使用
- 调整对应页面主题
确认FAB所在的Activity/Application使用的是Material Components系列主题,例如Theme.MaterialComponents.Light.NoActionBar,不可使用传统AppCompat主题,否则会导致Material组件渲染异常 - 清理缓存重新编译
执行Android Studio顶部菜单栏File > Invalidate Caches...操作,清理构建缓存后重新编译运行项目即可
内容的提问来源于stack exchange,提问作者Manoj
相关产品推荐
相关产品推荐

