Android - 将填充设置为RadioButton引脚

时间:2015-02-03 14:43:13

标签: android xml padding styling android-radiobutton

我已尝试过很多与xml和编程方式的组合,但还没有做任何事情。 这是场景(注意小红色箭头):

enter image description here

我在一个广播组中有一些RadioButtons,在xml中,这个场景可以表示为:

<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不工作)。

我的container_dropshadow.xml是:

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

3 个答案:

答案 0 :(得分:4)

我找到了一种方法,使用插图drawable:

首先,创建一个新的插图drawable(例如radio_button_inset.xml),其中包含您想要的填充和主题单选按钮的链接,如下所示:

<inset xmlns:android="http://schemas.android.com/apk/res/android"
      android:drawable="?android:attr/listChoiceIndicatorSingle"
      android:insetLeft="@dimen/your_padding_value"/>

其次,将这个新的drawable用于您的单选按钮:

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

答案 1 :(得分:0)

本文介绍了解决问题的方法,只需使用

RadioButton而不是plt.savefig("files/images/processed/" + self.get_league_name() + "_" + self.get_competition_name() + ".png", facecolor=self.background_color, dpi = 300) plt.show()

How to add padding before CheckBox/RadioButton across API levels

答案 2 :(得分:-1)

尝试在RadioGroup上使用paddingStart。由于它是从ViewGroup LinearLayout扩展而来的,因此所有子元素都会受到影响。

<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"
   android:paddingStart="8dp">
   <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>

在后台资源中尝试此操作。

<!-- Background -->
   <item>
      <shape>
         <padding android:left="8dp" />
         <solid android:color="@android:color/white" />
         <corners android:radius="3dp" />
      </shape>
   </item>