Android 布局 XML 中 app:srcCompat 和 android:src 的区别
Difference between app:srcCompat and android:src in Android's layout XML
每当我使用 Android Studio 的 Vector Assets 创建一个添加了图标的 ImageView 时,我在 app:srcCompat="@drawable/ic_play"
行收到错误
当我将 app:srcCompat
更改为 android:src
时,错误消失了,但图标看起来像素化了。
两者的主要区别是什么
app:srcCompat="@drawable/ic_play"
和
android:src="@drawable/ic_play"
矢量和动画矢量仅在最新版本的框架中受支持。 srcCompat 可以与兼容性库一起使用以使其工作,但这仅适用于支持库中的某些视图。请注意,使用的是 app: 而不是 android:。这意味着它不是框架的一部分,而是您的应用程序定义的参数。
app:srcCompat="some_resource"
指的是支持库中的 AppCompatActivity src 而
android:src="some_resource"
指简单activity。
将 AppCompat
与 ImageView
(或 ImageButton
和 FloatingActionButton
等子类)一起使用时,您将能够使用新的 app:srcCompat
属性引用矢量可绘制对象(以及 android:src
可用的任何其他可绘制对象)。如果您在运行时更改可绘制对象,您将能够使用与以前相同的 setImageResource()
方法(那里没有更改)。
使用 AppCompat
和 app:srcCompat
是将矢量绘图集成到您的应用程序中的最简单的方法。你会发现在 app:srcCompat
之外直接引用矢量绘图会在 Lollipop 之前失败。
Android 5.0(API 级别 21)及更高版本提供矢量可绘制对象支持,因此为了支持旧版本中的矢量可绘制对象,添加了 app:srcCompat
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 Support Library 23.3.0起,支持矢量图只能加载通过 app:srcCompat
.
您需要将 vectorDrawables.useSupportLibrary = true 添加到您的 build.gradle
文件
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
Sets a drawable as the content of this ImageView.It will display in
its original size. No automatic scaling .
使用:
app:srcCompat="@drawable/backImage"
srcCompat 属性实际上是在 AppCompat 库中定义的。
重要提示:您需要为此添加适当的命名空间。
xmlns:app="http://schemas.android.com/apk/res-auto"
备注
您得到的似乎只是一个可以忽略的 lint 错误。我试过并得到了同样的错误,但它工作正常。
您可以使用 tools:ignore="MissingPrefix"
来暂时避免看到此错误。
希望对您有所帮助。
将 AppCompat
与 ImageView
(或 ImageButton
和 FloatingActionButton
等子类)一起使用时,您将能够使用新的app:srcCompat
属性以引用旧版本平台上的矢量可绘制对象(以及 android:src
可用的任何其他可绘制对象).
android.support.v7.appcompat.R.attr.srcCompat:
srcCompat
Sets a drawable as the content of this ImageView. Allows the use of vector drawable when running on older versions of the platform.
May be a reference to another resource, in the form "@[+][package:]type/name"
or a theme attribute in the form "?[package:]type/name"
.
在使用app:srcCompat
的时候不要忘记加上xmlns:app="http://schemas.android.com/apk/res-auto"
。
如果您在 build.gradle 文件中使用没有 vectorDrawables.useSupportLibrary = true
的 android:src="@drawable/some_vector"
,并且您的应用程序有矢量图像(矢量可绘制),那么在构建 apk 文件 Android gradle 插件从每个矢量可绘制对象(仅适用于 API =< 19).结果 - apk.
的尺寸更大
将 app:srcCompat="@drawable/some_vector"
与 vectorDrawables.useSupportLibrary = true
一起使用时 android 使用矢量可绘制文件而不生成 *.png
文件。
您可以使用 Android Studio apk 分析器工具进行检查。只需在有和没有 vectorDrawables.useSupportLibrary = true
.
的情况下构建 apk
我认为这是主要区别。
每当我使用 Android Studio 的 Vector Assets 创建一个添加了图标的 ImageView 时,我在 app:srcCompat="@drawable/ic_play"
当我将 app:srcCompat
更改为 android:src
时,错误消失了,但图标看起来像素化了。
app:srcCompat="@drawable/ic_play"
和
android:src="@drawable/ic_play"
矢量和动画矢量仅在最新版本的框架中受支持。 srcCompat 可以与兼容性库一起使用以使其工作,但这仅适用于支持库中的某些视图。请注意,使用的是 app: 而不是 android:。这意味着它不是框架的一部分,而是您的应用程序定义的参数。
app:srcCompat="some_resource"
指的是支持库中的 AppCompatActivity src 而
android:src="some_resource"
指简单activity。
将 AppCompat
与 ImageView
(或 ImageButton
和 FloatingActionButton
等子类)一起使用时,您将能够使用新的 app:srcCompat
属性引用矢量可绘制对象(以及 android:src
可用的任何其他可绘制对象)。如果您在运行时更改可绘制对象,您将能够使用与以前相同的 setImageResource()
方法(那里没有更改)。
使用 AppCompat
和 app:srcCompat
是将矢量绘图集成到您的应用程序中的最简单的方法。你会发现在 app:srcCompat
之外直接引用矢量绘图会在 Lollipop 之前失败。
Android 5.0(API 级别 21)及更高版本提供矢量可绘制对象支持,因此为了支持旧版本中的矢量可绘制对象,添加了 app:srcCompat
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 Support Library 23.3.0起,支持矢量图只能加载通过 app:srcCompat
.
您需要将 vectorDrawables.useSupportLibrary = true 添加到您的 build.gradle
文件
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
Sets a drawable as the content of this ImageView.It will display in its original size. No automatic scaling .
使用:
app:srcCompat="@drawable/backImage"
srcCompat 属性实际上是在 AppCompat 库中定义的。 重要提示:您需要为此添加适当的命名空间。
xmlns:app="http://schemas.android.com/apk/res-auto"
备注
您得到的似乎只是一个可以忽略的 lint 错误。我试过并得到了同样的错误,但它工作正常。
您可以使用 tools:ignore="MissingPrefix"
来暂时避免看到此错误。
希望对您有所帮助。
将 AppCompat
与 ImageView
(或 ImageButton
和 FloatingActionButton
等子类)一起使用时,您将能够使用新的app:srcCompat
属性以引用旧版本平台上的矢量可绘制对象(以及 android:src
可用的任何其他可绘制对象).
android.support.v7.appcompat.R.attr.srcCompat:
srcCompat
Sets a drawable as the content of this ImageView. Allows the use of vector drawable when running on older versions of the platform.
May be a reference to another resource, in the form
"@[+][package:]type/name"
or a theme attribute in the form"?[package:]type/name"
.
在使用app:srcCompat
的时候不要忘记加上xmlns:app="http://schemas.android.com/apk/res-auto"
。
如果您在 build.gradle 文件中使用没有 vectorDrawables.useSupportLibrary = true
的 android:src="@drawable/some_vector"
,并且您的应用程序有矢量图像(矢量可绘制),那么在构建 apk 文件 Android gradle 插件从每个矢量可绘制对象(仅适用于 API =< 19).结果 - apk.
将 app:srcCompat="@drawable/some_vector"
与 vectorDrawables.useSupportLibrary = true
一起使用时 android 使用矢量可绘制文件而不生成 *.png
文件。
您可以使用 Android Studio apk 分析器工具进行检查。只需在有和没有 vectorDrawables.useSupportLibrary = true
.
我认为这是主要区别。