可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Whenever i create an ImageView with icon added using Android Studio's Vector asset i'm getting error at the line app:srcCompat="@drawable/ic_play"
And when i change the app:srcCompat="" with android:src="" the error is gone but the icon looks pixelated.
What is the main difference between app:srcCompat="@drawable/ic_play"
and android:src="@drawable/ic_play"
回答1:
is the most foolproof method of integrating
Note
As of support vector drawables can only be loaded via app:srcCompat .
you need to add vectorDrawables.useSupportLibrary = true to your build.gradle file // 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 .
回答2:
Vectors and animated vectors were only supported in recent versions of the framework. srcCompat can be used with the compatibility library to make them work, but this only works with the certain views in the support library. Notice that app: is used instead of android:. This means its not part of the framework, but a parameter defined by your app.
回答3:
Use:
app:srcCompat="@drawable/backImage"
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"
Note
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.
I hope this helps.
回答4:
app:srcCompat="some_resource"
is refer that it is AppCompatActivity src which comes in support library while android:src="some_resource"
refers to simple activity.
回答5:
When using AppCompat with ImageView (or subclasses such as ImageButton and FloatingActionButton), you’ll be able to use the new app:srcCompat attribute to reference vector drawables on older versions of the platform (as well as any other drawable available to android:src). 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".
Don't forget inserting xmlns:app="http://schemas.android.com/apk/res-auto" when use app:srcCompat.
回答6:
Android 5.0 (API level 21) and higher provides vector drawable support so in order to support vector drawables in older versions app:srcCompat was added
回答7:
when using AppCompat with ImageView (or subclasses such as ImageButton and FloatingActionButton), you’ll be able to use the new app:srcCompat attribute to reference vector drawables (as well as any other drawable available to android:src):And if you’re changing drawables at runtime, you’ll be able to use the same setImageResource() method as before - no changes there. Using AppCompat and app:srcCompat is the most foolproof method of integrating vector drawables into your app. You’ll find directly referencing vector drawables outside of app:srcCompat will fail prior to Lollipop.