首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何跨API级别在复选框/RadioButton之前添加填充

如何跨API级别在复选框/RadioButton之前添加填充
EN

Stack Overflow用户
提问于 2019-11-18 11:27:41
回答 1查看 704关注 0票数 1

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

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

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

代码语言:javascript
运行
复制
<?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. (如果没有希望的话)或2)最有创意的解决方案是什么?例如,这些视图左边的视图将触控事件传递给它们,或者使用填充的容器进行相同的操作。
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-14 13:42:02

我们发现,我们可以使用drawableStartdrawableLeftbuttonCompatdrawablePadding的组合来隐藏button,并显式地设置绘图:

代码语言:javascript
运行
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58913567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档