Android-将填充设置为RadioButton引脚 [英] Android - Set padding to RadioButton pin
问题描述
我已经尝试了很多与xml和以编程方式结合的方法,但是目前还没有任何事情.这是这种情况(请注意红色的小箭头):
I'have tried lot of combination both with xml and programmatically but nothing to do yet. This is the scenario (pay attention to the small red arrow):
我在单选组中有一些RadioButtons,在xml中,这种情况可以表示为:
I have some RadioButtons inside a radio group, in xml this scenario could be represented as:
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/radiobuttons"
android:paddingBottom="@dimen/activity_vertical_margin"
android:dividerPadding="30dp">
<RadioButton android:id="@+id/radio_blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="How to set the padding on the left of radio pin"
android:background="@drawable/container_dropshadow"/>
<!-- other radio buttons... -->
</RadioGroup>
我的问题是我既没有找到一种方法也无法在xml中或以编程方式来设置圆形图钉的左填充(我对两者都感兴趣).实际上,我在RadioButton对象中尝试修改的每个填充似乎都仅引用其中的文本(即Java函数setPadding和setCompoundDrawablePadding或xml android:paddingLeft和android:drawablePadding不起作用).
My problem is that I didn't find a way to set the left padding of the circle pin neither in xml nor programmatically (I'm interested in both). In fact each padding that I tried to modify in a RadioButton object seems to refer only to the text inside of it (i.e. java functions setPadding and setCompoundDrawablePadding, or xml android:paddingLeft and android:drawablePadding don't work).
我的container_dropshadow.xml是:
My container_dropshadow.xml is:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Drop Shadow Stack -->
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#00CCCCCC" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#10CCCCCC" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#20CCCCCC" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#30CCCCCC" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#50CCCCCC" />
</shape>
</item>
<!-- Background -->
<item>
<shape>
<solid android:color="@android:color/white" />
<corners android:radius="3dp" />
</shape>
</item>
</layer-list>
推荐答案
我找到了一种方法,可以使用嵌入式可绘制对象:
I found a way, by using an inset drawable:
首先,使用所需的填充和指向主题单选按钮可绘制对象的链接创建一个新的可绘制对象(例如radio_button_inset.xml),如下所示:
First, create a new inset drawable (e.g. radio_button_inset.xml) with your desired padding and a link to the theme radio button drawable like this:
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="?android:attr/listChoiceIndicatorSingle"
android:insetLeft="@dimen/your_padding_value"/>
第二,将此新的可绘制对象用于您的单选按钮:
Second, use this new drawable for your radio button:
<RadioButton android:id="@+id/radio_blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="How to set the padding on the left of radio pin"
android:background="@drawable/container_dropshadow"
android:button="@drawable/radio_button_inset"/>
这篇关于Android-将填充设置为RadioButton引脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!