2010-04-13 30 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下)。您也可以将它用作列表项目,并保持不变。