Android: What is the difference between app:srcCom

2019-01-02 20:54发布

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"

8条回答
千与千寻千般痛.
2楼-- · 2019-01-02 20:56

If you are using android:src="@drawable/some_vector" without vectorDrawables.useSupportLibrary = true in build.gradle file and you app have vector images (vector drawable), then while building apk file android gradle plugin generates a lot of *.png files for different screens (hdpi, xhdpi...) from each of your vector drawable. The result - bigger size of apk.

When using app:srcCompat="@drawable/some_vector" with vectorDrawables.useSupportLibrary = true android uses vector drawable files without generating *.png files.

You can check this with Android Studio apk analyzer tool. Just build apk with and without vectorDrawables.useSupportLibrary = true.

I think this is the main difference.

查看更多
姐姐魅力值爆表
3楼-- · 2019-01-02 20:59

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

查看更多
高级女魔头
4楼-- · 2019-01-02 21:01

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.

查看更多
一个人的天荒地老
5楼-- · 2019-01-02 21:09

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

Note

As of Android Support Library 23.3.0, 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  
    }  
 }  

android:src

Sets a drawable as the content of this ImageView.It will display in its original size. No automatic scaling .

查看更多
无色无味的生活
6楼-- · 2019-01-02 21:15

Use:

app:srcCompat="@drawable/backImage"

The srcCompat attribute is actually defined within AppCompat library. Important: you will need to add the appropriate namespace for this.

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

Note

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

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

I hope this helps.

查看更多
若你有天会懂
7楼-- · 2019-01-02 21:16
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.

查看更多
登录 后发表回答