如何在Android中使用VectorDrawable<;选择器>;

如何在Android中使用VectorDrawable<;选择器>;,android,kotlin,android-vectordrawable,Android,Kotlin,Android Vectordrawable,我试图使用VectorDrawable作为单选切换按钮,但在API 17级设备中出现“无效的可绘制标记向量””错误 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/ic_list_on" android:state_checked="true" /> <item android:drawabl

我试图使用VectorDrawable作为单选切换按钮,但在API 17级设备中出现“无效的可绘制标记向量””错误

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_list_on" android:state_checked="true" />
    <item android:drawable="@drawable/ic_list_off" android:state_checked="false" />
</selector>
另外,我设置了

vectorDrawables.useSupportLibrary = true
内建gradle

这是视图xml

<RadioGroup
    android:id="@+id/list_view_type"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="end"
    android:gravity="center_vertical|end"
    android:padding="15dp"
    android:orientation="horizontal">

    <android.support.v7.widget.AppCompatRadioButton
        android:id="@+id/list_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="15dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/grid_btn_toggle"
        android:button="@android:color/transparent"
        android:gravity="center"
        android:maxLines="1"
        android:text="" />

    <android.support.v7.widget.AppCompatRadioButton
        android:id="@+id/grid_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/list_btn_toggle"
        android:button="@android:color/transparent"
        android:checked="true"
        android:gravity="center"
        android:maxLines="1"
        android:text="" />
</RadioGroup>

使用
动画选择器

范例

使用此工具创建
动画矢量
文件


您应该将自定义的
可绘制的
应用于
android:button
属性,而不是
android:background

请添加build.gradle文件我认为从代码中删除AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)对您有用。看看这个。那可能会有帮助you@savepopulation我试过了,但还是没有luck@Expertwannabe你解决这个问题了吗?我现在就要面对它。
<RadioGroup
    android:id="@+id/list_view_type"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="end"
    android:gravity="center_vertical|end"
    android:padding="15dp"
    android:orientation="horizontal">

    <android.support.v7.widget.AppCompatRadioButton
        android:id="@+id/list_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="15dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/grid_btn_toggle"
        android:button="@android:color/transparent"
        android:gravity="center"
        android:maxLines="1"
        android:text="" />

    <android.support.v7.widget.AppCompatRadioButton
        android:id="@+id/grid_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/list_btn_toggle"
        android:button="@android:color/transparent"
        android:checked="true"
        android:gravity="center"
        android:maxLines="1"
        android:text="" />
</RadioGroup>
<vector android:height="24dp" android:viewportHeight="21.0"
    android:viewportWidth="21.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#c3c3c3" android:pathData="M2.5,0L2.5,0A2.5,2.5 0,0 1,5 2.5L5,2.5A2.5,2.5 0,0 1,2.5 5L2.5,5A2.5,2.5 0,0 1,0 2.5L0,2.5A2.5,2.5 0,0 1,2.5 0z"/>
    <path android:fillColor="#c3c3c3" android:pathData="M2.5,8L2.5,8A2.5,2.5 0,0 1,5 10.5L5,10.5A2.5,2.5 0,0 1,2.5 13L2.5,13A2.5,2.5 0,0 1,0 10.5L0,10.5A2.5,2.5 0,0 1,2.5 8z"/>
    <path android:fillColor="#c3c3c3" android:pathData="M2.5,16L2.5,16A2.5,2.5 0,0 1,5 18.5L5,18.5A2.5,2.5 0,0 1,2.5 21L2.5,21A2.5,2.5 0,0 1,0 18.5L0,18.5A2.5,2.5 0,0 1,2.5 16z"/>
    <path android:fillColor="#c3c3c3" android:pathData="M7,1.455L20,1.455a1,1 0,0 1,1 1L21,2.455a1,1 0,0 1,-1 1L7,3.455A0,0 0,0 1,7 3.455L7,1.455A0,0 0,0 1,7 1.455Z"/>
    <path android:fillColor="#c3c3c3" android:pathData="M7,9.455L20,9.455a1,1 0,0 1,1 1L21,10.455a1,1 0,0 1,-1 1L7,11.455A0,0 0,0 1,7 11.455L7,9.455A0,0 0,0 1,7 9.455Z"/>
    <path android:fillColor="#c3c3c3" android:pathData="M7,17.455L20,17.455a1,1 0,0 1,1 1L21,18.455a1,1 0,0 1,-1 1L7,19.455A0,0 0,0 1,7 19.455L7,17.455A0,0 0,0 1,7 17.455Z"/>
</vector>
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android {

    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    dataBinding {
        enabled = true
    }
    defaultConfig {
        applicationId "jp.dandoli.dandoliworks"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        debug {
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    compile 'com.android.support:design:26.1.0'
    compile 'com.android.support:support-v4:26.1.0'

    kapt "com.android.databinding:compiler:3.0.0-rc2"

    implementation "android.arch.lifecycle:runtime:1.0.0"
    implementation "android.arch.lifecycle:extensions:1.0.0-alpha9-1"

    implementation "com.android.support:cardview-v7:26.1.0"
    implementation "com.android.support:recyclerview-v7:26.1.0"

    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'io.reactivex.rxjava2:rxkotlin:2.1.0'

    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'
    compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'

    compile 'com.squareup.okhttp3:okhttp:3.8.1'
    compile 'com.squareup.okhttp3:logging-interceptor:3.4.0'

    compile 'com.google.code.gson:gson:2.8.0'

    compile 'com.squareup.picasso:picasso:2.5.2'
}