布局文件
<?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>
导致两个 RadioButton 平分水平空间。
这很好,但与其让它们向左齐平,不如让它们居中。为什么 android:gravity="center"
没有提供这种效果?
这题我试过earlier使用 CheckBoxes,但回复建议为每个 View (CheckBox) 插入一个无偿的 LinearLayout。我不能反对既不优雅也不高效的答案——只要它有效。
但是这个技巧不适用于 RadioButtons。 RadioGroup 必须是 RadioButton 集合的直接父级,以便它们在单击时相互取消选中。
编辑
通过上面的 XML 声明,我得到了下面的第一个草图。我不是在寻找前三个中的任何一个,而是在寻找第四个。
最佳答案
好吧,真是个坏消息,RadioButton
类实际上是硬编码的,radio drawable 将左对齐。我找到的唯一解决方案是继承 RadioButton 并自行处理 Drawable 的定位。
Here's替代解决方案(根本不使用 RadioGroup
)
关于android - 使 RadioButton View 居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16701806/