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

Android 6 Marshmallow使用Splash Screen API时渐变Drawable报错问题

Android 6 Marshmallow上Splash Screen API使用矢量Drawable出现“invalid color state list tag gradient”的解决方案

错误原因

Android 6(API 23)及更早版本的系统,不支持在<aapt:attr>节点内为矢量Drawable的fillColor属性直接定义<gradient>。这类版本的系统将fillColor严格限定为单一颜色值或ColorStateList,无法解析GradientDrawable类型的定义;而Android 7(API 24+)的系统则放宽了这个限制,允许直接在该属性中使用渐变配置。

解决方案

方案1:为低版本提供静态Drawable兼容资源

这是你提到的可行方案,具体操作步骤:

  • 在res目录下创建drawable-v23文件夹(对应API 23及以下版本,即Android 6及更早系统),如果已有drawable-v21,可以保留并将静态资源放入其中,让API 24+设备加载原矢量Drawable,低版本加载静态图。
  • 将带有渐变效果的矢量Drawable导出为PNG/JPG格式的静态图片,放入drawable-v23(或drawable-v21)文件夹,文件名与原矢量Drawable保持一致。
  • 确保Splash Screen的主题配置中引用该通用Drawable名称,系统会自动根据设备API版本加载对应资源:
    <!-- styles.xml中配置Splash Screen图标 -->
    <item name="android:windowSplashScreenAnimatedIcon">@drawable/your_splash_icon</item>
    

方案2:重构渐变定义(可选)

如果不想生成静态图,可尝试将渐变作为独立Drawable,通过fillDrawable属性引用,但仍需为低版本保留静态资源:

  1. 在drawable-v24文件夹中创建独立的渐变Drawable文件gradient_fill.xml:
    <gradient xmlns:android="http://schemas.android.com/apk/res/android"
        android:startX="45.94"
        android:startY="19.77"
        android:endX="107.75"
        android:endY="121.44"
        android:type="linear">
      <item android:offset="0" android:color="#FF5F9FFF"/>
      <item android:offset="0.27" android:color="#FFA9DCF9"/>
      <item android:offset="0.48" android:color="#FFC6EB98"/>
      <item android:offset="0.59" android:color="#FFDDE855"/>
      <item android:offset="0.7" android:color="#D1F4E50F"/>
      <item android:offset="0.91" android:color="#14DDFF0F"/>
    </gradient>
    
  2. 在drawable-v24的矢量Drawable中,用fillDrawable替代原来的fillColor配置:
    <vector xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:aapt="http://schemas.android.com/aapt"
            android:width="..."
            android:height="..."
            android:viewportWidth="..."
            android:viewportHeight="...">
      <path android:pathData="...">
        <aapt:attr name="android:fillDrawable">
          @drawable/gradient_fill
        </aapt:attr>
      </path>
    </vector>
    
  3. 同样在drawable-v23文件夹中放入对应静态图标,保证低版本设备正常显示。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 01:01:32