Android:将单选组内的单选按钮对齐到水平中心

问题描述 投票:0回答:1

我有一个由 Radio Group 组成的项目,它有 2 个水平方向的单选按钮。

这是 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ll_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/bg_border"
    android:orientation="horizontal">

    <RadioGroup
        android:id="@+id/rg_answer_truefalse"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:padding="0dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb_answer_true"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.25"
            android:background="@drawable/bg_border" />

        <RadioButton
            android:id="@+id/rb_answer_false"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.25"
            android:background="@drawable/bg_border" />
    </RadioGroup>

</LinearLayout>




问题是如何让每个单选按钮水平居中?我试过使用 layout_gravitygravity 作为中心或 center_horizontal 但它似乎不起作用。谢谢

java android xml kotlin radio-button
1个回答
0
投票

这是工作解决方案。不完美但有效。

<RadioGroup
    android:id="@+id/rg_answer_truefalse"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:weightSum="5">
        <View
            android:layout_height="1dp"
            android:layout_weight="1"
            android:layout_width="0dp" />
        
        <RadioButton
            android:id="@+id/rb_answer_true"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_width="0dp" />
        
        <View
            android:layout_height="1dp"
            android:layout_weight="1"
            android:layout_width="0dp" />
        
        <RadioButton
            android:id="@+id/rb_answer_false"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_width="0dp" />
        
        <View
            android:layout_height="1dp"
            android:layout_weight="1"
            android:layout_width="0dp" />
</RadioGroup>
© www.soinside.com 2019 - 2024. All rights reserved.