Can't use srcCompat for ImageViews in android

前端 未结 12 503
借酒劲吻你
借酒劲吻你 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 01:05

    Don't

    android:srcCompat="@drawable/wallpaper"
    

    Do

    app:srcCompat="@drawable/wallpaper"
    

    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"

    Important

    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.

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

    First (in build.gradle)

    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }
    

    Second

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/ic_traffic_black_24dp"
        tools:ignore="MissingPrefix" />
    

    Third

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

    Here we go.

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

    there is multiple things which you have to care about First of all use:

    defaultConfig {
                vectorDrawables.useSupportLibrary = true
            }
    

    or

    vectorDrawables {
            useSupportLibrary = true
        }
    

    and Secondly use

     <android.support.v7.widget.AppCompatImageView
                    android:id="@+id/changeLanguages"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:adjustViewBounds="true"
                    android:background="@color/transparent"
                    android:scaleType="fitCenter"
                    app:srcCompat="@drawable/ic_transfer" />
    

    Always use AppCompatImageView or Button because vector image is not supported by Simple ImageView

    if all the above methods not worked then use

    public class App extends Application {
    
      @Override public void onCreate() {
        super.onCreate();
    
        // Make sure we use vector drawables
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
      }
    }
    

    If you are using Activity then extend your Activity with AppCompatActivty

    public final class MainActivity extends AppCompatActivity {    
      @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_main);
      }
    }
    
    0 讨论(0)
  • 2020-12-03 01:21

    You are using android:srcCompat. It should instead by app:srcCompat as it is an attribute defined within AppCompat, not within the android: namespace.

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

    update your gradle plugin to 2.0+

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

    as per google developer's blog guid lines

    http://android-developers.blogspot.in/2016/02/android-support-library-232.html

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

    you need to add

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

    to your layout for it to work. My "app:srcCompat" was highlighted in red until i added it.

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