How to set VectorDrawable as an image for ImageView programmatically

后端 未结 8 1525
梦如初夏
梦如初夏 2020-12-13 05:58

I want to set some vectorDrawables to a ImageView in Android Studio.

I can set png and jpg drawable easily but when i want to set V

相关标签:
8条回答
  • 2020-12-13 06:23

    If you want to use vector drawables (less OR greater than API 21) just do the following:

    Set the image programmatically (e.g. in your activity):

    imageView.setImageResource(R.drawable.ic_left_arrow_blue); 
    

    or by XML:

    app:srcCompat="@drawable/your_vector_name"
    

    In your app's build.gradle you need to include:

    android {
        defaultConfig {
            vectorDrawables.useSupportLibrary = true
        }
    }
    

    And for vector support for less then API 21, add the following to onCreate:

    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);  
    
    0 讨论(0)
  • 2020-12-13 06:26

    Delete two folders form your drawable folder then rebuild your project it will work properly

    0 讨论(0)
提交回复
热议问题