更新Android Support库到23.2.0版本,使用srcCompat在API 21+上使用VectorDrawables。

3

非常棒的是,现在通过使用支持库 23.2.0,Lollipop 之前的设备也可以使用 VectorDrawables。虽然我在 API 21+ 上遇到了图像显示问题,但在低版本设备上一切正常。我正在使用 Gradle 插件 1.5,因此我的 build.gradle 包含以下内容:

// Gradle Plugin 1.5  
 android {  
   defaultConfig {  
     generatedDensities = []  
  }  

  // This is handled for you by the 2.0+ Gradle Plugin  
  aaptOptions {  
    additionalParameters "--no-version-vectors"  
  }  
 }  

然后我在我的布局中使用下面的代码:
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:srcCompat="@drawable/my_vector_drawable" />

我在父ViewGroup中声明了这个属性:

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

但是Android Studio仍然显示这个错误,但项目可以构建和运行

"意外的名称空间前缀“app”在标记ImageView中找到"

这就是我在Android 4.3上得到的结果: enter image description here

和Android 5.1: enter image description here

这是新支持库的一个bug还是我做错了什么?

2个回答

4
为解决您的图像缩放问题,您尝试将scaleType='fitXY'设置为ImageView吗?
(您现在可以安全地忽略该Lint错误,同时将tools:ignore="MissingPrefix"添加到您的ImageView中)。

0
“ImageView”标签中发现了意外的命名空间前缀“app””
您必须在布局中声明命名空间(通常在根元素中):
<ImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:srcCompat="@drawable/my_vector_drawable" />

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