显示多个单选按钮

Show many radio buttons

我在一个单选组下有 7 个单选按钮。我使用 orientation = "horizontal" 水平渲染它们。但一次只能看到 4 个,其余的则不可见。

有什么方法可以让我在第二行显示其余的,同时将所有这些 'Radiobuttons' 保留在一个“RadioGroup”中?

如果您需要我的代码 -

    <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1 BHK" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1.5 BHK" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2 BHK" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2.5 BHK" />
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2 BHK" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2.5 BHK" />
</RadioGroup>

您不能将 RadioButtons 放在屏幕上的不同位置,同时将它们保持在相同的 RadioGroup。您也不能将它们放在不同的行中(就像您想要实现的那样)。是的,我也讨厌它。

您可以制作复选框,按照您想要的方式放置它们,然后在每个复选框上使用 setOnCheckedChangedListener。因此,如果其中一个被选中,请对其他调用 setChecked(false)

使用自定义可绘制对象,使它们看起来像单选按钮而不是复选框。

您可以像这样在单选组中使用滚动视图

                 <RadioGroup
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:orientation="horizontal">
                <HorizontalScrollView
                   android:id="@+id/ScrlView"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content" >

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1 BHK" />

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1.5 BHK" />

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2 BHK" />

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2.5 BHK" />
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2 BHK" />

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2.5 BHK" />
                     </ScrollView>
</RadioGroup>

我想,没有足够的地方容纳所有的人。尝试将 android:layout_width="0dp"android:layout_weight="1" 设置为所有 RadioButton。但是可以不怎么好看

尝试为所有单选按钮设置宽度

 android:layout_width="0dp"

并为每个单选按钮添加 android:layout_weight="1"

 <RadioButton
                 android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="1 BHK" 
android:layout_weight="1"/>

从您的 xml 代码中替换以下代码

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1 BHK" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1.5 BHK" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2 BHK" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2.5 BHK" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2 BHK" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2.5 BHK" />
        </RadioGroup>
    </LinearLayout>
</HorizontalScrollView>

我的解决方案是在 RadioGroup 布局中添加一个 LinearLayout。 我将 RadioGroup 的方向更改为垂直并添加 两个水平方向的 LinearLayout 将作为行提供服务。

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"        
    >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1 BHK" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1.5 BHK" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2 BHK" />

    </LinearLayout>


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

         <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2.5 BHK" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3 BHK" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3.5 BHK" />

    </LinearLayout>
</RadioGroup>

我也在 RadioGroup 中尝试这个,你一次只能 select 一个它很好用:

<RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton android:layout_height="match_parent" >
        </RadioButton>

        <RadioButton android:layout_height="match_parent" >
        </RadioButton>

        <RadioButton android:layout_height="match_parent" >
        </RadioButton>

        <RadioButton android:layout_height="match_parent" >
        </RadioButton>

        <RadioButton android:layout_height="match_parent" >
        </RadioButton>

        <RadioButton android:layout_height="match_parent" >
        </RadioButton>

        <RadioButton android:layout_height="match_parent" >
        </RadioButton>
    </RadioGroup>