2010-04-13 55 views

回答

9

正如Ravi Vyas所示,您可以使用TextViewRadioButton自己完成此操作。 RadioButton沒有什麼內在的東西來重新定位按鈕相對於文本,從我的源代碼閱讀。

此外,請記住,僅僅因爲這可能並不意味着這是一個好主意。例如,在iPhone上,如果您太過於混淆您的應用,則可能不允許發佈您的應用,因爲它們具有應用必須遵守的人機界面準則。 Android給了你更多的繩索 - 不要讓你的用戶掛在它上面。

16

我想做同樣的事情,而不必又擴展另一個類(或兩個,因爲你將不得不至少延長CompoundButton 單選)的東西,應該是從一開始就執行工作的一部分。因爲我使用的是RadioGroup,如果你在佈局容器中放置RadioButton和TextView,它將不起作用。毫無疑問,我的解決方案有點冒失,但 - 它的工作原理。

1)設置填充留給40 2)設置佈局保證金留給-36dp

此時,原來的單選按鈕將視圖外,和你的文本視圖會坐在最左邊有4dp的餘量。

3)設置爲@android可繪製權:可繪製/ btn_radio

現在你必須在左側文字和右側的按鈕,本地單選按鈕,將與一個RadioGroup中工作。

@CommonsWare

值得一提的是,這是令人難以置信的諷刺帶來了人機界面指南針對這一特定問題。尤其是考慮到調整RadioButton佈局以將按鈕放置在最右側,將與Spinner菜單的佈局保持一致。我完全同意你對此事的看法 - 但NickTFried很有可能試圖彌補Android在這方面的「懸掛」問題。

+5

您可以使用android:button =「@ null」刪除左側單選按鈕(而不是步驟1) – EtienneSky 2012-06-20 06:29:23

+0

當我設置android:按鈕=「@ null」,單選按鈕沒有被檢查(點擊)。我做錯了什麼。請幫助http://stackoverflow.com/questions/28052820/android-custom-radio-button-not-getting-checked – 2015-01-20 18:54:44

8

如果您使用單個「單選按鈕」,CheckedTextView可正常工作。

但我需要保留一個組中的單選按鈕中的觸發功能,所以這對我更好地工作:

 <RadioGroup 
     android:id="@+id/radioGroup1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <RadioButton 
      android:id="@+id/radioButton1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:button="@null" 
      android:drawableRight="@android:drawable/btn_radio" 
      android:gravity="left|center_vertical" 
      android:layout_marginLeft="-32dp" 
      android:text="Radio Button 1" /> 

     <RadioButton 
      android:id="@+id/radioButton2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:button="@null" 
      android:drawableRight="@android:drawable/btn_radio" 
      android:gravity="left|center_vertical" 
      android:layout_marginLeft="-32dp" 
      android:text="Radio Button2" /> 

    </RadioGroup> 

根據需要調整android:layout_marginLeft

6

對於RTL語言i。ËArabic使用:

 <RadioGroup 
      android:id="@+id/rgNewsFilter" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

      <RadioButton 
       android:id="@+id/rbAllNews" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="-32dp" 
       android:button="@null" 
       android:drawableRight="@android:drawable/btn_radio" 
       android:gravity="right|center_vertical" 
       android:text="ذنيسبمنشخصث" 
       android:textColor="#ffffff" /> 

      <RadioButton 
       android:id="@+id/rbMyTeam" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="-32dp" 
       android:button="@null" 
       android:drawableRight="@android:drawable/btn_radio" 
       android:gravity="right|center_vertical" 
       android:text="تشسيبتسيتبتسيب" 
       android:textColor="#ffffff" /> 
     </RadioGroup> 
0

的Android有一個內置的佈局,你可以使用 - android.R.layout.simple_list_item_single_choice

<?xml version="1.0" encoding="utf-8"?> 
<!-- Copyright (C) 2008 The Android Open Source Project 

    Licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 
    You may obtain a copy of the License at 

      http://www.apache.org/licenses/LICENSE-2.0 

    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License. 
--> 

<CheckedTextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="?android:attr/listPreferredItemHeightSmall" 
    android:textAppearance="?android:attr/textAppearanceListItemSmall" 
    android:gravity="center_vertical" 
    android:checkMark="?android:attr/listChoiceIndicatorSingle" 
    android:paddingStart="?android:attr/listPreferredItemPaddingStart" 
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" /> 

如果要修改它,你可以在上面的複製粘貼到一個新的XML文件,然後使用<include>標籤將其包含在其他佈局中(在Apache 2.0 License下)。您也可以將它用作列表項目,並保持不變。