首页 > 解决方案 > android:button drawable for radiobutton未在模拟器中加载

问题描述

我想创建自定义 RadioButtons 但我遇到了问题。当我在 Android Studio 中预览我的工作时,一切似乎都很好,但在模拟器中我的可绘制对象没有正确加载。我错过了什么/做错了什么?我的代码如下所示:

XML RadioGroup:

    `<RadioGroup
    android:id="@+id/ec_radiogroup"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="15dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="15dp"
    android:layout_marginBottom="10dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/ic_ec_switch">

    <RadioButton
        style="@style/ec_radiobuttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:button="@drawable/it_radiobutton_sunrise"
        android:padding="16dp"
        android:text="Morgen" />

    <RadioButton
        style="@style/ec_radiobuttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:button="@drawable/it_radiobutton_sun"
        android:padding="16dp"
        android:text="Mittag" />

    <RadioButton
        style="@style/ec_radiobuttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:button="@drawable/it_radiobutton_sunrise"
        android:padding="16dp"
        android:text="Abend" />
</RadioGroup>`

XML 可绘制(android:button="@drawable/it_radiobutton_sunrise"):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_checked="true"
    android:drawable="@drawable/ic_radiobutton_sunrise_active"/>
<item
    android:state_checkable="true"
    android:drawable="@drawable/ic_radiobutton_sunrise_active"/>
<item
    android:state_focused="true"
    android:drawable="@drawable/ic_radiobutton_sunrise_active"/>
<item
    android:state_activated="true"
    android:drawable="@drawable/ic_radiobutton_sunrise_active"/>
<item
    android:state_pressed="true"
    android:drawable="@drawable/ic_radiobutton_sunrise_active"/>
<item
    android:state_selected="true"
    android:drawable="@drawable/ic_radiobutton_sunrise_active"/>
<item
    android:state_checked="false"
    android:drawable="@drawable/ic_radiobutton_sunrise_inactive" />
<item
    android:state_checkable="false"
    android:drawable="@drawable/ic_radiobutton_sunrise_inactive" />
<item
    android:state_focused="false"
    android:drawable="@drawable/ic_radiobutton_sunrise_inactive" />
<item
    android:state_activated="false"
    android:drawable="@drawable/ic_radiobutton_sunrise_inactive" />
<item
    android:state_pressed="false"
    android:drawable="@drawable/ic_radiobutton_sunrise_inactive" />
<item
    android:state_selected="false"
    android:drawable="@drawable/ic_radiobutton_sunrise_inactive" />
</selector>

它应该是什么样子:它应该是什么样子 但它看起来像这样:看起来像这样

标签: androidxmlandroid-layoutradio-buttonandroid-drawable

解决方案


我在一些较旧的 android 版本上遇到了类似的问题,请尝试将其添加到模块 build.gradle 。

    android {
defaultConfig {
    vectorDrawables.useSupportLibrary = true
}}

推荐阅读