2010-01-25 84 views
58

在仍然使用Android内置组件的情况下,是否可以在RadioButton和标签之间添加一点空间?默认情况下,文本看起来有点sc。。在Android中的RadioButton和它的标签之间添加边距?

<RadioButton android:id="@+id/rb1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="My Text"/> 

我试了几件事情:

  1. 指定margin和padding似乎增加整个元素空间(按钮和文本,一起)。这是有道理的,但不会做我需要的。

  2. 创建一个自定义绘图通过XML指定图像的选中和未选中状态,然后在每个图像的右侧添加一些额外的像素。这应该可以工作,但现在你已经走出了默认的用户界面。 (不是世界的尽头,但不是理想的)

  3. 在每个标签的开头添加额外的空格。 Android似乎修剪了一个前导空格字符,就像在“我的字符串”中那样,但是在“\ u00A0My字符串”中指定了Unicode字符U + 00A0也是可以的。这工作,但它似乎有点肮脏。

任何更好的解决方案?

回答

5

现在无法尝试此验证,但是您是否尝试了解属性android:drawablePadding是否能满足您的需求?

+1

嗯。试试看,在这种情况下似乎没有任何影响。感谢这个建议,从描述中看,它确实听起来很正确:“可绘制和文本之间的填充”。 – allclaws 2010-01-26 01:07:44

56

不知道这是否会解决您的问题,但你尝试单选按钮的“填充左”性质与50dip以上

+1

这可以工作,需要50dip来覆盖默认drawable的宽度。起初我用20dip,文字左移!这是因为锚点不是可绘制的右边缘;它是drawable的左边缘。 – 2011-04-06 05:39:58

+1

此解决方案似乎工作 – 2015-03-17 17:03:58

0

我都试过了,值“机器人:paddingLeft”将作品。 paddingLeft只会影响文本,同时使广播图像保持原始位置。

51

我尝试几种方法,并与这一个两个模拟器和设备工作正常完成:

<RadioButton 
     android:background="@android:color/transparent" 
     android:button="@null" 
     android:drawableLeft="@drawable/your_own_selector" 
     android:drawablePadding="@dimen/your_spacing" /> 
  • 安卓背景必须是透明的,因为它似乎在api10有与内在背景其他apis不尝试,但解决方案也适用于其他人)
  • android:button需要为null因为填充将无法正常工作其他rwise
  • 安卓drawableLeft需要指定的,而不是安卓按钮
  • 安卓drawablePadding是,这将是你的绘制和文本
+4

最佳答案!你也可以设置android:background =“@ null”,看起来就像你没有填充一样。 – saiyancoder 2014-01-13 23:23:12

+2

当我设置android:button =“@ null”时,单选按钮没有被检查(点击)。我做错了什么。请帮助http:// stackoverflow。com/questions/28052820/android-custom-radio-button-not-getting-checked – 2015-01-20 18:55:52

+0

@Shirish这个解决方案与你的按钮没有被点击没有关系。我刚刚实现它,并且它被点击。 – Yar 2015-01-27 16:26:25

0

的“机器人之间的空间: paddingLeft“似乎只能在Android 4.2.2下正常工作

我已经尝试了几乎所有版本的Android,它只能在4.2.2版本上工作。

87

现在任何人阅读此。接受的答案会导致较新的apis上出现一些布局问题,从而导致填充过多。

在API < = 16上,您可以在单选按钮上设置填充以设置相对于单选按钮视图边界的填充。此外,补丁9背景也会相对于视图更改视图边界。

在API 17上,左填充与可绘制单选按钮有关。这同样适用于大约九个补丁。为了更好地说明填充差异,请参阅附加的屏幕截图。

如果您深入了解代码,您将在api 17中找到一种名为getHorizontalOffsetForDrawables的新方法。在计算单选按钮的左填充时(因此图中显示了额外的空间),将调用此方法。

TL; DR你应该有最小的SDK要支持单选按钮的风格和另一种风格的API 17+

combine screen shots showing left padding differences between api versions

+2

api 17 is android.os.Build.VERSION_CODES.JELLY_BEAN_MR1 – KarenAnne 2013-08-02 06:48:17

+10

最好的方法是使用“values-v17”目录;把你的API 17+维度放在一个资源XML中,对于16或更低的维度,只需要简单的“值”即可。 – akent 2014-11-11 04:47:53

+12

令人惊讶的是Google开发人员/管理层如何对待我们平常的开发人员。他们随时都可以随心所欲地做任何事情。一致性和完整性对​​他们毫无意义。 – Yar 2015-01-27 16:27:58

-1

我用不同的方法,我想对所有API应该工作版本。 代替将填补我加入到单选按钮之间的空观点:

<View 
     android:layout_width="20dp" 
     android:layout_height="1dp" /> 

这应该给你20dp填充。

4
final float scale = this.getResources().getDisplayMetrics().density; 
checkBox.setPadding(checkBox.getPaddingLeft() + (int)(10.0f * scale + 0.5f), 

    checkBox.getPaddingTop(), 
    checkBox.getPaddingRight(), 
    checkBox.getPaddingBottom()); 
1

绘图和文本之间的填充。这将通过在xml文件中添加下面的行来实现。 android:drawablePadding="@dimen/10dp"

+0

我一直在寻找这个......帮了我很多 – 2017-05-17 11:52:10

+0

'@ dimen/10dp'不好的练习 – 2017-09-28 09:04:14

22

通过paddingLeft添加保证金一个单选按钮标签之间其

android:paddingLeft="10dip" 

只需设置您的自定义填充

RadioButton的xml属性。

<RadioButton 
    android:id="@+id/radHighest" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:button="@drawable/YourImageResource" 
    android:drawablePadding="50dp" 
    android:paddingLeft="10dip" 
    android:text="@string/txt_my_text" 
    android:textSize="12sp" /> 

完成

0

我来到这里寻找答案,(经过一番思考)最简单的方法是在标签本身的开头添加间距像这样

<RadioGroup 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignRight="@+id/btnChangeMode" 
    android:layout_marginTop="10dp" 
    android:layout_marginBottom="10dp" 
    android:layout_below="@+id/view3" 
    android:gravity="center|left" 
    android:id="@+id/ledRadioGroup"> 
<RadioButton 
     android:button="@drawable/custom_radio_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text=" On" 
     android:layout_marginRight="6dp" 
     android:id="@+id/ledon" 
     android:textColor="@color/white" /> 
<RadioButton 
     android:button="@drawable/custom_radio_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text=" Off" 
     android:layout_marginLeft="6dp" 
     android:id="@+id/ledoff" 
     android:textColor="@color/white" /> 
0

为我的作品:

<?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item android:state_checked="true"> 
      <layer-list> 
       <item android:right="5dp"> 
        <shape android:paddingLeft="5dp" android:shape="oval"> 
        <size android:width="20dp" android:height="20dp" /> 
        <solid android:color="@color/blue" /> 
      </shape> 
     </item> 
    </layer-list> 
</item> 
<item android:paddingLeft="5dp" android:state_checked="false"> 
    <layer-list> 
     <item android:right="5dp"> 
      <shape android:paddingLeft="5dp" android:shape="oval"> 
       <size android:width="20dp" android:height="20dp" /> 
       <solid android:color="@color/grey" /> 
       </shape> 
      </item> 
     </layer-list> 
    </item> 
</selector> 
10

使用以下XML属性秒。它的工作对我来说

对于API < = 16使用

android:paddingLeft="16dp" 

对于API> = 17使用

android:paddingStart="@16dp" 

如:

<android.support.v7.widget.AppCompatRadioButton 
     android:id="@+id/popularityRadioButton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:checked="true" 
     android:paddingEnd="@dimen/radio_button_text" 
     android:paddingLeft="@dimen/radio_button_text" 
     android:paddingRight="@dimen/radio_button_text" 
     android:paddingStart="@dimen/radio_button_text" 
     android:text="Popularity" 
     android:textSize="@dimen/sort_dialog_text_size" 
     android:theme="@style/AppTheme.RadioButton" /> 

enter image description here

更多内容:drawablePadding属性不起作用。它只有在您的单选按钮中添加了drawable时才有效。对于如:

<RadioButton 
    android:id="@+id/radioButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:button="@null" 
    android:drawableEnd="@android:drawable/btn_radio" 
    android:drawablePadding="56dp" 
    android:drawableRight="@android:drawable/btn_radio" 
    android:text="New RadioButton" /> 
0
<RadioButton 
       android:id="@+id/rb1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="5dp" 
       android:background="@null" 
       android:paddingLeft="20dp" 
       android:text="1" 
       android:textColor="@color/text2" 
       android:textSize="16sp" 
       android:textStyle="bold" /> 
+0

考虑添加一些注释来解释如何解决这个问题;) – DeadlyJesus 2017-03-10 10:50:59

0

你可以尝试使用单选按钮的重力属性。

<RadioButton 
         android:id="@+id/rb_all" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:checked="true" 
         android:gravity="right|center_vertical" 
         android:padding="@dimen/padding_30dp" 
         android:text="@string/filter_inv_all" 
         android:textColor="@color/black" 
         android:textSize="@dimen/text_size_18" /> 

这将使文本与最右端对齐。查看图像中的第一个广播。

enter image description here

0

您可以在XML文件中的代码

<RadioButton 
android:id="@+id/rButton" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:drawablePadding="50dp" 
android:paddingLeft="10dip" 
android:text="@string/your_text" /> 

或使用上的活动类

radioButton.setPadding(12, 10, 0, 10); 
0

利用重力

  android:gravity="center_horizontal|center_vertical" 
0

style.xml创建一个样式像这样

<style name="Button.Radio"> 

    <item name="android:paddingLeft">@dimen/spacing_large</item> 
    <item name="android:textSize">16sp</item> 
</style> 

把这种风格在单选按钮

<RadioButton 
       android:id="@+id/rb_guest_busy" 
       android:layout_width="match_parent" 
       android:layout_height="48dp" 
       android:text="@string/guest_is_waiting" 
       android:textSize="@dimen/font_size_3x_medium" 
       android:drawablePadding="@dimen/spacing_large" 
       android:textColor="@color/color_text_heading_dark" 
       style="@style/Button.Radio"/> 

你可以改变一样按钮的任何属性,因为它RADIOBUTTON间接继承按钮。

0

我知道这是一个老问题,但有了这个解决方案,我终于安心,忘了API的水平。

左侧垂直RadioGroup与右侧垂直文本视图。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <RadioGroup 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical"> 
     <RadioButton 
      android:id="@+id/radio1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
     <RadioButton 
      android:id="@+id/radio2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
    </RadioGroup> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="8dp" 
      android:gravity="center_vertical" 
      android:text="Radio 1"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="8dp" 
      android:gravity="center_vertical" 
      android:text="Radio 2"/> 
    </LinearLayout> 
</LinearLayout>