显示许多单选按钮

时间:2015-04-10 06:18:06

标签: android android-radiobutton

我在一个广播组下面有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>

7 个答案:

答案 0 :(得分:1)

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

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

使用自定义drawable,使它们看起来像单选按钮而不是复选框。

答案 1 :(得分:0)

您可以在这样的广播组中使用scrollview

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

答案 2 :(得分:0)

我想,所有人都没有足够的地方。尝试将android:layout_width="0dp"android:layout_weight="1"设置为所有RadioButtons。但它可能不是很漂亮。

答案 3 :(得分:0)

尝试设置所有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"/>

答案 4 :(得分:0)

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

答案 5 :(得分:0)

我也在RadioGroup内尝试这个,你可以一次只选择一个它很好用: enter image description here

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

答案 6 :(得分:-1)

我的解决方案是在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>
相关问题