【问题标题】:android:button drawable for radiobutton not loading in emulatorandroid:button drawable for radiobutton未在模拟器中加载
【发布时间】:2021-08-09 14:37:34
【问题描述】:

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

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>

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

【问题讨论】:

  • 简单方式&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:drawable="@drawable/ic_radiobutton_sunrise_active" android:state_checked="true" /&gt; &lt;item android:drawable="@drawable/ic_radiobutton_sunrise_inactive" android:state_checked="false" /&gt; &lt;/selector&gt;
  • 不幸的是简化选择器xml文件并没有解决问题:/

标签: android xml android-layout radio-button android-drawable


【解决方案1】:

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

    android {
defaultConfig {
    vectorDrawables.useSupportLibrary = true
}}

【讨论】:

  • 感谢您的提示!不幸的是它仍然不起作用:/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-30
相关资源
最近更新 更多