Android 布局 XML 中 app:srcCompat 和 android:src 的区别 [英] Difference between app:srcCompat and android:src in Android's layout XML
问题描述
每当我使用 Android Studio 的矢量资源创建带有图标的 ImageView 时,我都会在 app:srcCompat="@drawable/ic_play"
Whenever I create an ImageView with icon added using Android Studio's Vector Assets, I'm getting an error at the line app:srcCompat="@drawable/ic_play"
当我用 android:src
更改 app:srcCompat
时,错误消失了,但图标看起来像素化了.
When I change the app:srcCompat
with android:src
, the error is gone but the icon looks pixelated.
app:srcCompat="@drawable/ic_play"
和
android:src="@drawable/ic_play"
推荐答案
是集成矢量绘图 到您的应用程序中.Vector drawables 允许您用 XML 中定义的单个矢量图形替换多个 png 资产.虽然以前仅限于 Lollipop 和更高版本的设备
is the most foolproof method of integrating vector drawables into your app.Vector drawables allow you to replace multiple png assets with a single vector graphic, defined in XML. While previously limited to Lollipop and higher devices
注意
截至 Android 支持库 23.3.0, 支持向量可绘制对象只能通过 app:srcCompat
加载.
As of Android Support Library 23.3.0, support vector drawables can only be loaded via app:srcCompat
.
您需要将 vectorDrawables.useSupportLibrary = true 添加到您的 build.gradle
文件
you need to add vectorDrawables.useSupportLibrary = true to your build.gradle
file
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
设置一个drawable作为这个ImageView的内容.它将显示在它的原始大小.没有自动缩放.
Sets a drawable as the content of this ImageView.It will display in its original size. No automatic scaling .
这篇关于Android 布局 XML 中 app:srcCompat 和 android:src 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!