围绕单选意见 [英] Centering RadioButton Views
问题描述
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MyLL"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center" />
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center" />
</RadioGroup>
</LinearLayout>
导致两个单选按钮同样分裂水平空间。
results in the two RadioButtons splitting the horizontal space equally.
这是好的,但不是让他们刷新到左边,我希望他们为中心。为什么的android:比重=中心
不会放弃这种效果
That is good, but rather than having them flush to the left, I would like them to be centered. Why is android:gravity="center"
not giving this effect?
我已经试过了这个问题早些时候与复选框但回复提示插入一个无偿的LinearLayout每个视图(复选框)。我不能反驳的答案既不优雅,也不高效,只要它的作品。
I have tried this question earlier with CheckBoxes, but the replies suggested inserting a gratuitous LinearLayout for each View (CheckBox). I can't argue against an answer that is neither elegant nor efficient—as long as it works.
但是,这招不与单选按钮使用。该RadioGroup中必须为单选按钮被点击时他们取消相互集合的直接父。
But that trick does not work with RadioButtons. The RadioGroup must be the direct parent of a collection of RadioButtons for them to uncheck each other when clicked.
修改
随着XML声明上面我得到的第一个下面的草图。我不找任何前三,但第四。
With the XML declarations above I am getting the first of the sketches below. I am not looking for any of the first three, but for the fourth.
推荐答案
好吧,那么伤心的消息,单选
类实际上是硬codeD无线电提拉将左对齐。我发现的唯一的解决办法是继承单选按钮和手柄定位在自己的可绘制。
Okay, so sad news, the RadioButton
class is actually hard-coded that the radio drawable will be aligned to the left. The only solution I've found is to subclass RadioButton and handle positioning a Drawable on your own.
这里是一个的替代解决方案(不使用 RadioGroup中
在所有)
Here's an alternate solution (without using RadioGroup
at all)
这篇关于围绕单选意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!