Android 居中单选按钮视图

Android 居中单选按钮视图,android,android-layout,Android,Android Layout,布局文件 <?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="w

布局文件

<?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>

结果是两个单选按钮平均分割水平空间

这很好,但我希望它们居中,而不是向左齐平。为什么android:gravity=“center”没有产生这种效果

我试过用复选框来回答这个问题,但是回答建议为每个视图插入一个免费的线性布局(复选框)。我不能反对一个既不优雅也不高效的答案,只要它有效

但这种技巧对单选按钮不起作用。RadioGroup必须是RadioButton集合的直接父对象,以便在单击时彼此取消选中

编辑

通过上面的XML声明,我得到了下面的第一个草图。我不是在寻找前三个,而是第四个


好吧,不幸的消息是,
RadioButton
类实际上是硬编码的,可拖动收音机将向左对齐。我找到的唯一解决方案是对RadioButton进行子类化,并自行定位一个Drawable

替代解决方案(根本不使用
RadioGroup


@空的
?android:ListChoiceIndicator单个
居中

您需要将重心放在放射组上,而不是我看到的按钮上。这很有意思,但是把模拟单选按钮或普通复选框居中怎么样?必须为每个小部件添加一个额外的LinearLayout以使其居中吗?不,您可以只使用具有复合drawable的TextView,并为TextView提供一个重心。因此,问题是单选按钮在内部由drawable和text的LinearLayout父项组成,但drawable硬连接到(内部)的左侧线性布局。如果是这样的话,有没有一种方法可以访问内部drawable并设置其重力?可能使用反射,但在这种情况下使用它并不安全,因为RadioButton的内部结构可能会在不同的OS版本之间发生变化。
<style name="TextlessRadioButton" parent="android:Widget.Material.CompoundButton.RadioButton">
    <item name="android:button">@null</item>
    <item name="android:foreground">?android:listChoiceIndicatorSingle</item>
    <item name="android:foregroundGravity">center</item>
</style>