我想在RadioButton
和CheckBox
视图的左边/开始添加填充。这样做的原因是要有全宽度可选列表(用于选择一个并选择多个),其中单选按钮或复选框元素在它们与屏幕边缘之间有空间,但也不会在该空间所在的不可点击区域结束。
对于API 16及以下版本,我可以添加间距并保持可点击性,我可以使用视图的paddingLeft
属性。对于API 17及以上,该属性控制按钮/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" />
然而,这似乎最终抵消了点击反馈(涟漪)。所以有几个问题:
发布于 2020-01-14 13:42:02
我们发现,我们可以使用drawableStart
、drawableLeft
、buttonCompat
和drawablePadding
的组合来隐藏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>
https://stackoverflow.com/questions/58913567
复制相似问题