2012-09-24 29 views
10

我想要「標籤」, - 您在RadioButton中設置的文本顯示在按鈕的左側並在其間有一些填充。在Android上有一個邊距的RadioButton文本左側

添加額外TextView到佈局不工作,因爲我RadioGroup不工作(我可以選擇多個按鈕),如果我添加任何其他然後RadioButtonRadioGroup

那麼,怎樣才能改變我的單選按鈕通過設置android:button="@null"並添加RadioButton的繪製爲android:drawableRight<text><buttondrawable>而不是<buttondrawable><text>

回答

36

你可以做到這一點。您可以使用android:drawablePadding更改文本和繪圖之間的填充。

<RadioGroup 
     android:id="@+id/radios" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:inputType="text" 
     android:orientation="vertical" > 

     <RadioButton 
      android:id="@+id/first" 
      android:button="@null" 
      android:drawableRight="@android:drawable/btn_radio" 
      android:drawablePadding="20dp" 
      android:layout_width="200dip" 
      android:layout_height="wrap_content" 
      android:text="@string/first" 
      android:textSize="20dip" /> 

     <RadioButton 
      android:id="@+id/second" 
      android:button="@null" 
      android:drawableRight="@android:drawable/btn_radio" 
      android:drawablePadding="20dp" 
      android:layout_width="200dip" 
      android:layout_height="wrap_content" 
      android:text="@string/second" 
      android:textSize="20dip" /> 
    </RadioGroup> 
</RelativeLayout> 
+0

有兩個那些內部的一個RadioGroup,我仍然可以選擇兩個。 –

+0

我已經更新了我的答案。請立即檢查。 –

+0

謝謝!非常聰明的解決方案,我可以很容易地用'android:drawablePadding'添加我想要的填充。 –

1

上面的答案使圖像沒有選擇狀態。

願這可以幫助您:

  • 在API < = 16你可以設置填充留在單選按鈕相對於單選按鈕的觀點邊界填充設置。此外,補丁9背景也會相對於視圖更改視圖邊界。

  • 在API 17上,左填充與可繪製單選按鈕有關。這同樣適用於大約九個補丁。爲了更好地說明填充差異,請參閱附加的屏幕截圖。

如果您深入瞭解代碼,您將在api 17中找到一個名爲getHorizo​​ntalOffsetForDrawables的新方法。在計算單選按鈕的左填充時(因此圖中顯示了額外的空間),將調用此方法。

TL; DR你應該有最小的SDK要支持單選按鈕的風格和另一種風格的API 17+

的回答是從this same question回答@詹姆斯·巴卡

+0

請在代碼片段中添加精華,未來的讀者不應該遵循您的鏈接,除非他們想要更深入的信息。 –

+0

此外,答案的順序可能會有所不同,所以最好參考海報名稱的答案。 –

+0

感謝您的建議@JeroenKransen – actsai

相關問題