How to add padding before CheckBox/RadioButton across API levels

允我心安 提交于 2021-01-24 14:05:03

问题


I want to add padding to the left/start of RadioButton and CheckBox views. The motivation for this is to have full width selectable lists (for select one and select multiple) where the radio button or check box element have space between themselves and the screen edge but also don't end up with a non clickable area where that space is.

For API 16 and below I can add the spacing AND maintain clickability I can use the view's paddingLeft attribute. For API 17 and above this attribute controls the padding between the button/box element and it's corresponding text (which of course we also want to control).

For API 21 and above I can use a drawable inset like so to create the desired layout:

<?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" />

This however appears to end up offsetting click feedback (the ripple). So a few questions:

  1. Is there a better way to create this spacing across all (16 and up really) API levels?
  2. If no hope for 1), is there a better way of creating this spacing for API 21 and above that doesn't mess with the ripple feedback?
  3. If there is no hope for 1) or 2) what is the most creative solution to this? For example, a view to the left of these views that propogates touch events to them or a container with padding that does the same thing.

回答1:


We found that we could create the spacing across API levels using a combination of drawableStart, drawableLeft, buttonCompat and drawablePadding to hide the button and explicitly set the 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://stackoverflow.com/questions/58913567/how-to-add-padding-before-checkbox-radiobutton-across-api-levels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!