单选按钮的可绘制对象无法响应state_checked状态

4
我正在尝试在一个 RadioButton 中创建一个勾选标志,如果未选择该按钮,则该标志会消失。但是,如果我将自定义的勾选标记放在 RadioButtonandroid:button 属性中,它似乎只对 state_checked 作出反应,这是出乎意料的。因为如果我用 android:drawableRight="?android:attr/listChoiceIndicatorSingle" 替换自定义的勾选标记,则这个方法就非常完美正常工作了。包含在 drawableRight 中的勾选标记是我首选的,因为在 android:button 属性上设置间距是有问题的。
是否存在不同的设置来使勾选标记按预期行为?
以下是当前代码:
<RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:button="@null"
            android:drawableRight="@drawable/period_selector"
            android:background="?android:selectableItemBackground"
            android:layoutDirection="rtl"
            android:layout_gravity="start"
            android:textAlignment="textStart"
            android:paddingVertical="16dp"
            android:paddingHorizontal="16dp"
            android:text="@string/period_selection_month"
            android:textSize="16sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="?divider" />

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:button="@null"
            android:drawableRight="@drawable/period_selector"
            android:background="?android:selectableItemBackground"
            android:layoutDirection="rtl"
            android:layout_gravity="start"
            android:textAlignment="textStart"
            android:paddingVertical="16dp"
            android:paddingHorizontal="16dp"
            android:text="@string/period_selection_year"
            android:textSize="16sp" />
</RadioGroup>

使用以下代码drawable/period_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_check" android:state_checked="true" />
</selector>

总结如下:

  • 无论按钮是否选中,使用drawableRight中的自定义打勾标志都不会呈现。
  • drawableRight中使用标准的listChoiceIndicatorSingle可以正确响应按钮是否被选中。
  • button中使用自定义打勾标志可以显示预期的选择行为(出现和消失),但不会监听间距。
1个回答

3
问题

<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/ic_check" android:state_checked="true" /> </selector>

您的选择器缺失了未选中状态的RadioButton,因此它将drawableRight设置为没有可绘制的内容。因此,drawableRight的默认占用大小为0。

当您将按钮状态更改为已选中时,它会尝试将ic_check绘制到当前保留的drawableRight大小为0的区域中,因此您看不到任何变化。

解决方案

要修复此问题,您需要指定选择器的未选中状态为某个可绘制的内容。

我的尝试使用@null或使用0大小,但两者都会失败,并出现异常,因为未指定可绘制的内容或其大小为0。

因此,解决此问题的方法是使用与ic_check相同但具有透明度的可绘制内容。

这是Android Studio提供的默认勾选标记可绘制内容:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:tint="@color/teal_700"
    android:viewportWidth="24"
    android:viewportHeight="24">

    <path
        android:fillColor="@color/teal_700"
        android:pathData="M9,16.17L4.83,12l-1.42,1.41L9,19 21,7l-1.41,-1.41z" 

</vector>

接着创建另一个与典型图标相似的,但使用透明颜色的图标:ic_check_empty.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24"
    android:tint="@android:color/transparent">
    <path
        android:fillColor="@android:color/transparent"
        android:pathData="M9,16.17L4.83,12l-1.42,1.41L9,19 21,7l-1.41,-1.41z"/>
</vector>

并将其应用于选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_check" android:state_checked="true" />
    <item android:drawable="@drawable/ic_check_empty" android:state_checked="false" />
</selector>

副笔记:RadioGroup中使用View将引发异常,因为这里不允许;如果你需要在单选按钮之间添加分隔符,则可以使用:

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="?android:attr/dividerHorizontal"
    android:showDividers="middle"> 

或者您可以在这里查看其他解决方案


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接