android - 使 RadioButton View 居中

标签 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="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 声明,我得到了下面的第一个草图。我不是在寻找前三个中的任何一个,而是在寻找第四个。

enter image description here

最佳答案

好吧,真是个坏消息,RadioButton 类实际上是硬编码的,radio drawable 将左对齐。我找到的唯一解决方案是继承 RadioButton 并自行处理 Drawable 的定位。

Here's替代解决方案(根本不使用 RadioGroup)

关于android - 使 RadioButton View 居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16701806/

相关文章:

android - android中的Glassmorphism效果

Android 线性布局 : Layout inside a Layout

java - 布局小不被识别/不工作

android - 正在使用适合长时间运行的网络调用的 WorkManager

android - Handler 中 post(Runnable) 和 sendMessage(Message) 的区别

android - 执行 eglSwapBuffer 和 eglMakeCurrent 时性能低下

android - 将 TextView 在 ImageView 内水平居中,Imageview 旁边还有另一个 View

android - 如何换行 Android TextView View ?

android - 调试端口 (8700) 正忙,请确保没有其他 Activity 的调试连接到同一应用程序?

android - 如何禁用 EditText 的自动完成和自动更正