无效的可绘制标签矢量图。

44

我正在尝试在Android Lolipop之前的设备上使用矢量图。我按照这里的说明操作了,但仍然遇到崩溃问题。

build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0-beta6'
    }
}
apply plugin: 'com.android.application'

repositories {
    maven { url 'http://maven.android-forever.com' }
    jcenter()
}

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.test.app"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.android.support:design:23.2.0'
    compile "de.greenrobot:eventbus:2.4.0"
    compile 'de.greenrobot:greendao:2.1.0'
    compile "com.af:android-utility:1.0.0.9"
    compile project(':volleyplus')
    compile project (':libvlc')
}

三角形.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/triangle_v"/>
</selector>

triangle_v.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="100dp"
    android:width="100dp"
    android:viewportHeight="100"
    android:viewportWidth="100">

<path
    android:name="triangle"
    android:fillColor="#FF0000"
    android:pathData="m 50,0 l 50,100 -100,0 z"/>

</vector>

布局.xml

<ImageView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@drawable/triangle"/>

我还尝试过使用app:srcCompat,但是这种情况下,可绘制物就是不显示。


1
我注意到在selector中使用向量和作为布局背景会导致这些异常。 - Someone Somewhere
14个回答

1
如果你在使用drawableLeft/drawableStart等遇到了这个问题,有一个简单的解决方法,如果你正在使用数据绑定。
不要写成:

而是:

android:drawableLeft="@drawable/somevector"

做:

android:drawableLeft="@{@drawable/somevector}"

这是因为数据绑定将在编译时生成代码,以兼容的方式检索可绘制对象。

在应用程序中调用 AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) 也是必需的。但是你的解决方案完美地解决了问题。 - JP Ventura
这给了我一个构建错误,提示“找不到与给定名称匹配的资源(位于'value'为'@{@drawable/.....}的'src'处”。 - Le-roy Staines

1
这是support-v4、appcompat-v7库v23.2.0的bug,它出现在API 19中。 @tim提供了此bug问题的链接。
您可以升级到新的库版本23.2.1,现在该bug已经修复。

0

0
如果您在加载矢量图时使用<android.support.v7.widget.AppCompatImageView,那么最好扩展AppCompatActivity而不是Activity,并返回使用常规的<ImageView...app:srcCompat="@drawable/...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接