如何在 API 级别的 CheckBox/RadioButton 之前添加填充

How to add padding before CheckBox/RadioButton across API levels

我想向 RadioButtonCheckBox 视图的 left/start 添加填充。这样做的动机是拥有全宽 select 可用列表(对于 select 一个和 select 多个),其中单选按钮或复选框元素之间有 space 和屏幕边缘,但也不会以 space 所在的不可点击区域结束。

对于 API 16 及以下我可以添加间距并保持可点击性 我可以使用视图的 paddingLeft 属性。对于 API 17 及更高版本,此属性控制 button/box 元素与其对应文本(当然我们也想控制)之间的填充。

对于 API 21 及以上,我可以像这样使用可绘制插图来创建所需的布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.AppCompatRadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/margin_standard"
    android:button="@drawable/inset_radio_button"
    tools:text="Option A">

</androidx.appcompat.widget.AppCompatRadioButton>

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="?android:attr/listChoiceIndicatorMultiple"
    android:insetLeft="@dimen/margin_standard" />

然而,这似乎最终抵消了点击反馈(涟漪)。那么几个问题:

  1. 是否有更好的方法在所有(实际上是 16 岁及以上)API 关卡中创建此间距?
  2. 如果对 1) 没有希望,是否有更好的方法来为 API 21 及更高版本创建此间距且不会干扰纹波反馈?
  3. 如果 1) 或 2) 没有希望,对此最创意 的解决方案是什么?例如,这些视图左侧的视图向它们传播触摸事件,或者带有填充的容器执行相同的操作。

我们发现我们可以使用 drawableStartdrawableLeftbuttonCompatdrawablePadding 的组合来创建跨 API 个级别的间距来隐藏button 并显式设置可绘制对象:

<androidx.appcompat.widget.AppCompatRadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/margin_standard"
    android:paddingStart="@dimen/margin_standard"
    android:paddingRight="@dimen/margin_standard"
    android:paddingEnd="@dimen/margin_standard"
    android:paddingTop="@dimen/margin_small"
    android:paddingBottom="@dimen/margin_small"
    android:drawableStart="?android:attr/listChoiceIndicatorSingle"
    android:drawableLeft="?android:attr/listChoiceIndicatorSingle"
    android:button="@null"
    app:buttonCompat="@null"
    android:drawablePadding="@dimen/margin_standard">

</androidx.appcompat.widget.AppCompatRadioButton>