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

我想在RadioButtonCheckBox视图的左侧/开始添加填充。这样做的动机是要有全宽的可选列表(用于选择一个并选择多个),其中单选按钮或复选框元素在其自身和屏幕边缘之间有间隔,但不要以该间隔为不可单击的区域结尾是。

对于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)没有希望,那么最创造性的解决方案是什么?例如,这些视图左侧的一个视图向其传播触摸事件,或者是一个带有填充的容器,其作用相同。
caiczy000 回答:如何跨API级别在CheckBox / RadioButton之前添加填充

我们发现我们可以使用drawableStartdrawableLeftbuttonCompatdrawablePadding的组合来跨API级别创建间距,以隐藏button和明确设置drawable:

<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>
本文链接:https://www.f2er.com/3081519.html

大家都在问