app:srcCompat - Vector drawable shows up in design preview, but doesn't show up in app

心不动则不痛 提交于 2020-01-11 09:10:12

问题


drawable/information.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:fillColor="#FF000000"
        android:pathData="M13,9H11V7H13M13,17H11V11H13M12,2A10,10 0,0 0,2 12A10,10 0,0 0,12 22A10,10 0,0 0,22 12A10,10 0,0 0,12 2Z"/>
</vector>

LayoutofCardView.xml

<ImageView
    android:layout_width="24dp"
    android:background="#f00"
    android:layout_height="24dp"
    android:id="@+id/informationImageView"
    app:srcCompat="@drawable/information"
    tools:src="@drawable/information"
    />

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.company"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        generatedDensities = []

    }
    aaptOptions {
        additionalParameters "--no-version-vectors"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE.txt'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:cardview-v7:23.2.1'
    compile 'com.android.support:recyclerview-v7:23.2.1'
    compile 'com.android.support:support-v4:23.2.1'
}

The image shows up correctly in the design pane of android studio. But when I run the app, the image doesn't show up. The image is in cardView, which is in recyclerviewfragment. other items render correctly only vector drawbles don't render.

Also, Lint error shows up in the layout file

unexpected namespace prefix "app" found for tag ImageView app:srcCompat

回答1:


Insert this line vectorDrawables.useSupportLibrary = true into your build.graddle file as part of the defaultConfig statement. I've used the descriptions that are provided here and it works. Think you have to be aware that config looks different for Gradle 2.0+ and Gradle 1.5.

Regardless of the Gradle version you're using, you have to ensure that that your activity has to extend AppCompatActivity to provide vector support.




回答2:


I found this answer when I have your problem. Quick answer is you should use android.support.v7.widget.AppCompatImageView and app:srcCompat

this link explain it.



来源:https://stackoverflow.com/questions/36220868/appsrccompat-vector-drawable-shows-up-in-design-preview-but-doesnt-show-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!