Can't use srcCompat for ImageViews in android

前端 未结 12 502
借酒劲吻你
借酒劲吻你 2020-12-03 00:38

I\'m using the Design Support Library 23.2. I\'ve added these lines in my build.gradle as my Gradle Plugin is version 1.5

defaultConfig {
        applicat         


        
相关标签:
12条回答
  • 2020-12-03 00:59

    Combining many answers into one answer:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:background="@color/primary">
    
    
        <android.support.v7.widget.AppCompatImageView
            android:layout_width="300dp"
            android:layout_height="100dp"
            android:id="@+id/logoImageView"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            app:srcCompat="@drawable/my_logo_vector"
            android:tint="@color/white"
            />
    
    </RelativeLayout>
    

    This worked for me. No lint errors. Use AppCompatImageView.

    0 讨论(0)
  • 2020-12-03 00:59

    I know am late but It might help someone else. I was facing the same issue even though I was using app:SrcCompat.After spending a lot of time I figured it was due to drawables. My issue simply resolved by moving all drawables from drawable(v21) to simple drawable.

    0 讨论(0)
  • 2020-12-03 01:02

    Make sure the picture you are trying to use is PNG format.

    Copy the picture.

    Go to mipmap on the 1.Project TAB in the android hierarchy.

    Click on mipmap once then(On windows ctrl + v ), when you name the photo make sure its lower case letters and no symbols.

    The photo should be useable.

    Set your image view, (you may have to use a filler color temporarily).

    Click on the imageView and then click the srcCompact (the one without the paintbrush and type on @mipmap/"Your pictures name" (This step is in the attributes tab)

    That should work!

    If you need a Content Description I just used @String/StringPicture

    0 讨论(0)
  • 2020-12-03 01:03

    Call app:srcCompat instead of android:srcCompat .

    Don't

    android:srcCompat="@drawable/your_image"
    

    DO

    app:srcCompat="@drawable/your_image"
    

    Finally

        <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/wallpaper"/>
    

    My gradle version is 1.5

    Upgrade your Gradle version.

    Solution

    Gradle Plugin 2.0+  
    com.android.tools.build:gradle:2.0.0-beta2
    

    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, both VectorDrawable and AnimatedVectorDrawable are now available through two new Support Libraries support-vector-drawable and support-animated-vector-drawable.

    Add this

        // Gradle Plugin 2.0+  
     android {  
       defaultConfig {  
         vectorDrawables.useSupportLibrary = true  
        }  
     } 
    

    You’ll note this new attribute only exists in the version 2.0 of the Gradle Plugin. If you are using Gradle 1.5 you’ll instead use

    // Gradle Plugin 1.5  
     android {  
       defaultConfig {  
         generatedDensities = []  
      }  
    
      // This is handled for you by the 2.0+ Gradle Plugin  
      aaptOptions {  
        additionalParameters "--no-version-vectors"  
      }  
     }  
    

    Go Through Support Vector Drawables

    0 讨论(0)
  • 2020-12-03 01:03

    From, Android developer site

    This library is now a dependency of the v7 AppCompat library, allowing developers and AppCompat to easily use vector drawables.

    To use VectorDrawableCompat within an ImageButton or ImageView, use the app:srcCompat XML attribute or setImageResource() method.

    To keep referencing attribute IDs on API level 20 or lower, add the following appt flag to your build,gradle file:

    If you are building with Android Plugin for Gradle 1.5.0 or lower, add the following to your build.gradle file:

    android {
      defaultConfig {
        // Stops the Gradle’s automatic rasterization of vectors
        generatedDensities = []
      }
       // Flag that tells aapt to keep the attribute ids
      aaptOptions {
        additionalParameters "--no-version-vectors"
      }
    }
    

    If you are building with Android Plugin for Gradle 2.0.0 or higher, add the following to your build.gradle file:

    android {
      defaultConfig {
        vectorDrawables.useSupportLibrary = true
      }
    }
    
    0 讨论(0)
  • 2020-12-03 01:04

    I just changed:

    app:srcCompat="@drawable/wallpaper"
    

    to

    android:src="@drawable/wallpaper"
    

    This worked for me.

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