使用RadioGroup和RadioButtons

时间:2011-04-14 17:08:12

标签: java android

嘿,我想让RadioButton的文本显示在RadioButton的左侧..所以我尝试使用2个相对视图(我有2个RadioButtons)并且它工作但问题是RadioGroup无法正常工作。继承密码:

<RelativeLayout android:id="@+id/SearchRelative" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">

    <RadioButton android:checked="true" 
        android:id="@+id/SearchRadioButtonAloneWord" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true">
    </RadioButton>

    <TextView android:text="1" 
        android:layout_toLeftOf="@+id/SearchRadioButtonAloneWord"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </TextView>
</RelativeLayout>

<RelativeLayout android:id="@+id/SearchRelative" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">

    <RadioButton android:id="@+id/SearchRadioButtonInBetweenWord"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true">
    </RadioButton>

    <TextView android:text="2"
        android:layout_toLeftOf="@+id/SearchRadioButtonInBetweenWord"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </TextView>
</RelativeLayout>

它们与RadioButton左侧的文字正确显示..但RadioGroup无法正常工作!
我该如何解决这个问题? 谢谢

Edit1 :是否可以动态地将RadioButton添加到群组中?这是解决我问题的一种方法!

1 个答案:

答案 0 :(得分:4)

关于在RadioGroup中使用布局管理器:对不起,它不起作用。据我所知,如果某个内容添加到RadioGroup,而RadioButton不是RadioGroup的实例,则不会由此RadioButton管理。但这提出了一个问题的替代解决方案:扩展RadioGroup以便按照您想要的方式绘制它。

至于动态操作:是的,ViewGroup就可以像使用任何其他RadioButton一样。您所要做的就是通过RadioGroup.addView()添加{{1}}。

相关问题