2010-01-25 169 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>