【问题标题】:How to add padding before CheckBox/RadioButton across API levels如何在 API 级别的 CheckBox/RadioButton 之前添加填充
【发布时间】:2019-11-18 11:27:41
【问题描述】:

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

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

对于 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) 没有希望,那么最创造性的解决方案是什么?例如,这些视图左侧的视图向它们传播触摸事件,或者具有填充的容器执行相同的操作。

【问题讨论】:

    标签: android android-layout android-view android-xml


    【解决方案1】:

    我们发现我们可以使用 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      • 1970-01-01
      • 2011-12-12
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 2019-06-11
      相关资源
      最近更新 更多