Radiobutton drawable 对 state_checked 没有反应
Radiobutton drawable not reacting to state_checked
我正在尝试在 RadioButton
中创建一个复选标记,如果未选中该按钮,该复选标记就会消失。但是,如果我将自定义复选标记放在 RadioButton
的 android: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
中的自定义复选标记确实显示了出现和消失的预期选择行为,但不听间距
问题
<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;因此您不会看到任何变化。
解决方案
要解决此问题,您需要将选择器的未选中状态指定为某些可绘制对象。
My trials to use @null
or to using 0 size, but both fails with
exceptions as the drawable is not specified or it's 0 in size.
因此,解决这个问题的方法是使用与 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
将引发异常,因为此处不允许这样做;如果你需要在 RadionButtons 之间添加一个分隔符,那么你可以使用:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle">
或者你可以看看here for other solutions.
我正在尝试在 RadioButton
中创建一个复选标记,如果未选中该按钮,该复选标记就会消失。但是,如果我将自定义复选标记放在 RadioButton
的 android: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
中的自定义复选标记确实显示了出现和消失的预期选择行为,但不听间距
问题
<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;因此您不会看到任何变化。
解决方案
要解决此问题,您需要将选择器的未选中状态指定为某些可绘制对象。
My trials to use
@null
or to using 0 size, but both fails with exceptions as the drawable is not specified or it's 0 in size.
因此,解决这个问题的方法是使用与 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
将引发异常,因为此处不允许这样做;如果你需要在 RadionButtons 之间添加一个分隔符,那么你可以使用:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle">
或者你可以看看here for other solutions.