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

Android中LottieAnimationView背景颜色无法修改怎么办?

解决LottieAnimationView莫名显示白色背景的问题

下面是几个可行的解决思路:

  • 用Lottie专属背景属性替代系统background
    别再依赖android:background了,Lottie有自己的背景配置属性app:lottie_backgroundColor,直接指定透明或者你需要的背景色即可,比如:

    app:lottie_backgroundColor="@android:color/transparent"
    

    如果要适配深色模式,直接填入你的@color/background资源就行。

  • 再次确认动画文件的实际背景
    哪怕你认为动画背景是透明的,也建议用Lottie在线预览工具打开动画文件检查——有时候导出动画时会不小心带上白色背景层,这种情况只能重新导出透明背景的版本。

  • 调整ScaleType参数
    你当前使用的是centerCrop,如果动画尺寸和View尺寸不匹配,空白区域可能会被自动填充白色。换成fitCenter或者centerInside试试:

    android:scaleType="fitCenter"
    
  • 代码中强制设置透明背景
    如果XML配置无效,就在代码里直接修改:

    // 方式1:通过系统API设置背景透明
    anim_start_btn.setBackgroundColor(Color.TRANSPARENT)
    // 方式2:通过Lottie自身API设置
    anim_start_btn.setLottieBackgroundColor(Color.TRANSPARENT)
    

修改后的参考XML代码

<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/anim_start_btn"
    android:layout_width="@dimen/_110sdp"
    android:layout_height="@dimen/_40sdp"
    android:scaleType="fitCenter"
    app:lottie_speed="1.2"
    app:lottie_rawRes="@raw/start_btn"
    app:lottie_autoPlay="false"
    app:lottie_loop="false"
    app:lottie_backgroundColor="@color/background"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toTopOf="@id/tv_start_instruction"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    />

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 21:32:25