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

Android FloatingActionButton设置图片及绿色tint后仍显红色问题求助

解决FloatingActionButton图标颜色不生效的问题

嘿,我来帮你搞定这个问题!你设置了android:tint="@color/green"但FAB图标还是显示红色,大概率是属性用错了或者有其他优先级更高的设置在干扰,咱们一步步排查:

  • 优先改用app:tint替代android:tint
    很多自定义的FloatingActionButton组件(包括官方Material Design的版本)都是用app命名空间的tint属性来控制图标颜色的,而不是原生的android:tint。这是因为组件内部的ImageView可能绑定了app命名空间的属性,所以把代码里的tint属性改个命名空间试试:

    <com.xxx.test.fab.FloatingActionButton
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ikona2"
        app:tint="@color/green" <!-- 替换成app命名空间 -->
        app:layout_anchor="@id/mapview"
        app:layout_anchorGravity="bottom|left|end" />
    
  • 检查你的图标资源格式
    如果你的@drawable/ikona2是JPG格式的图片,tint属性可能无法正常生效——因为JPG是不透明的位图,系统很难对它应用颜色滤镜。建议把图标转换成PNG或者VectorDrawable格式,再重新设置tint。

  • 用代码动态设置(兜底方案)
    如果布局设置还是不生效,试试在代码里直接设置图标 tint,这种方式优先级更高,能覆盖大部分主题或组件默认设置:
    比如Kotlin代码:

    val fab = findViewById<FloatingActionButton>(R.id.list)
    fab.imageTintList = ContextCompat.getColorStateList(this, R.color.green)
    

    或者Java代码:

    FloatingActionButton fab = findViewById(R.id.list);
    fab.setImageTintList(ContextCompat.getColorStateList(this, R.color.green));
    

先试试第一个方案,这是最常见的解决办法,应该能解决你的问题!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:22:03