android - 使用 VectorDrawableCompat

标签 android vector support-vector-compat

为了在我的 android 项目中使用 SVG 文件,我必须使用 vector compat。

首先,我根据 http://a-student.github.io/SvgToVectorDrawableConverter.Web/ 将我的 SVG 文件转换为矢量

然后关注了官博http://android-developers.blogspot.fr/2016/02/android-support-library-232.html

这是我的build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "xxx"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 22
        versionName "2.0"
        vectorDrawables.useSupportLibrary = true
    }
    [...]
}

在我的 xml 中我使用这个

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dark_grey"
    tools:ignore="MissingPrefix">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/login_margin">

        <ImageView
            android:id="@+id/info_about_app"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:background="@drawable/back_login"
            app:srcCompat="@drawable/vector"/>  <-- Important line

我的矢量看起来像

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:auto="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:width="425dp"
        android:height="252dp"
        android:viewportHeight="504"
        android:viewportWidth="850"
        auto:height="252dp"
        auto:viewportHeight="504"
        auto:viewportWidth="850"
        auto:width="425dp"
        tools:ignore="NewApi">
    <group
        android:translateY="-548.3622"
        auto:translateY="-548.3622">

在 Android Studio 中预览没有问题,但是当我尝试运行我的应用时,出现以下错误

Error:(2) No resource identifier found for attribute 'viewportHeight' in package 'xxx'

我认为这个错误是由于错误的 ids 向后移植造成的,但它是

的作用
vectorDrawables.useSupportLibrary = true

android plugin for gradle ==> 2.1.0-beta1

我该如何解决?

最佳答案

只需删除这一行:

auto:viewportHeight="504"

这就是 No resource identifier found for attribute 'viewportHeight' 给我的建议。

或者您没有导入支持向量兼容库。
但我倾向于认为你做到了。

关于android - 使用 VectorDrawableCompat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36661737/

相关文章:

android - 针对不同设备的不同 Espresso 测试

algorithm - 垂直于 3D 点集合的方向?

c++ - std::forward_iterator_tag 的作用是什么?

excel - Matlab 数据预处理和动态结构分配

android - 这个Android Studio项目的build.gradle中声明了哪些第三方依赖?

android 上的 java.lang.NoClassDefFoundError

java - 电话应用程序 : item cannot be resolved or is not a field

android - SASLError 使用 PLAIN : not-authorized