无法在 android 中将 srcCompat 用于 ImageViews [英] Can't use srcCompat for ImageViews in android

查看:12
本文介绍了无法在 android 中将 srcCompat 用于 ImageViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用设计支持库 23.2.我在 build.gradle 中添加了这些行,因为我的 Gradle 插件是 1.5 版

defaultConfig {应用程序IDcom.abc.xyz"minSdkVersion 16targetSdkVersion 23版本代码 1版本名称1.0"生成密度 = []}aaptOptions {附加参数--no-version-vectors"}}

因为它在 这里 中指定/p>

但是我的图像视图不能使用 srcCompat 属性.

其中@drawable/wallpaper 是一个矢量资源文件

上面写着

<块引用>

错误:(14) 未找到属性srcCompat"的资源标识符包'android'

我的 Gradle 版本是 1.5.如何使用 srcCompat?

解决方案

不要

android:srcCompat="@drawable/wallpaper"

app:srcCompat="@drawable/wallpaper"

因为它的 srcCompat 属性实际上是在 AppCompat 库中定义的.

重要提示您需要为此添加适当的命名空间.

<块引用>

xmlns:app="http://schemas.android.com/apk/res-auto"

重要

<块引用>

你得到的似乎只是一个 lint 错误,可能是忽略.我已经尝试过并且有同样的错误,但它正在工作正确.

您可以使用 tools:ignore="MissingPrefix" 暂时避免看到此错误.

I'm using the Design Support Library 23.2. I've added these lines in my build.gradle as my Gradle Plugin is version 1.5

defaultConfig {
        applicationId "com.abc.xyz"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        generatedDensities = []
    } 

      aaptOptions {  
        additionalParameters "--no-version-vectors"  
      }  
     }

as it's specified in here

But I can't use the srcCompat attribute for my imageview.

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:srcCompat="@drawable/wallpaper"/>

where @drawable/wallpaper is a vector resource file

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
<path
    android:fillColor="#FF000000"
    android:pathData="M4,4h7V2H4c-1.1,0 -2,0.9 -2,2v7h2V4zm6,9l-4,5h12l-3,-4 -2.03,2.71L10,13zm7,-4.5c0,-0.83 -0.67,-1.5 -1.5,-1.5S14,7.67 14,8.5s0.67,1.5 1.5,1.5S17,9.33 17,8.5zM20,2h-7v2h7v7h2V4c0,-1.1 -0.9,-2 -2,-2zm0,18h-7v2h7c1.1,0 2,-0.9 2,-2v-7h-2v7zM4,13H2v7c0,1.1 0.9,2 2,2h7v-2H4v-7z"/>

It says

Error:(14) No resource identifier found for attribute 'srcCompat' in package 'android'

My Gradle version is 1.5. How can I use srcCompat?

解决方案

Don't

android:srcCompat="@drawable/wallpaper"

Do

app:srcCompat="@drawable/wallpaper"

as it srcCompat attribute is actually defined within AppCompat library.

Important you will need to add appropriate namespace for this.

xmlns:app="http://schemas.android.com/apk/res-auto"

Important

what you are getting it seems like it is just a lint error that can be ignored. I have tried and have the same error, but it is working correctly.

you can use tools:ignore="MissingPrefix" to avoid seeing this error temporarily.

这篇关于无法在 android 中将 srcCompat 用于 ImageViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆