如何跨API级别在CheckBox/RadioButton之前添加填充 [英] How to add padding before CheckBox/RadioButton across API levels

查看:93
本文介绍了如何跨API级别在CheckBox/RadioButton之前添加填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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).

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

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. 是否有更好的方法在所有(16个及以上)API级别上创建此间距?
  2. 如果对1)没有希望,是否有更好的方法为API 21及更高版本创建此间距,而不会与纹波反馈混为一谈?
  3. 如果没有希望1)或2)的最创意解决方案是什么?例如,这些视图左侧的一个视图向其传播触摸事件,或者是一个带有填充的容器,其作用相同.
  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.

推荐答案

我们发现,可以结合使用 drawableStart drawableLeft buttonCompat drawablePadding 隐藏 button 并显式设置drawable:

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>

这篇关于如何跨API级别在CheckBox/RadioButton之前添加填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆