单选按钮自定义方向

时间:2018-07-17 07:13:01

标签: android

我有一个RadioGroup,方向为水平。我想在同一行中显示3个RadioButton,在另一行中显示最后一个RadioButton,bcoz小屏幕电话无法在同一行中显示所有按钮。我希望所有的RadioButton都位于同一RadioGroup中。我该如何实现?I want to achieve like this

<RadioGroup
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
       <RadioButton
           android:id="@+id/rb_oval"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Oval"
           android:checked="false"
           android:buttonTint="@color/colorGreen"/>
       <RadioButton
           android:id="@+id/rb_circle"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Circle"
           android:checked="false"
           android:buttonTint="@color/colorGreen"/>
       <RadioButton
           android:id="@+id/rb_oblong"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Oblong"
           android:checked="false"
           android:buttonTint="@color/colorGreen"/>
       <RadioButton
           android:id="@+id/rb_rectangle"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Square/Rectangle"
           android:checked="false"
           android:buttonTint="@color/colorGreen"
        />
</RadioGroup>

2 个答案:

答案 0 :(得分:0)

  

请仔细阅读这段代码,希望对您有帮助

  <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <RadioButton
                android:id="@+id/rb_oval"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Oval"
                android:checked="false"
                android:buttonTint="@color/colorPrimary"/>
            <RadioButton
                android:id="@+id/rb_circle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Circle"
                android:checked="false"
                android:buttonTint="@color/colorPrimaryDark"/>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <RadioButton
                    android:id="@+id/rb_oblong"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Oblong"
                    android:checked="false"
                    android:buttonTint="@color/colorAccent"/>
                <RadioButton
                    android:id="@+id/rb_rectangle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Square/Rectangle"
                    android:checked="false"
                    android:buttonTint="@color/colorPrimaryDark"></RadioButton>
            </LinearLayout>

        </RadioGroup>
    </LinearLayout>

答案 1 :(得分:0)

尝试这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

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

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

<RadioButton
android:id="@+id/rb_oval"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:buttonTint="@color/colorGreen"
android:checked="false"
android:layout_weight="1"
android:text="Oval" />

<RadioButton
android:id="@+id/rb_circle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:buttonTint="@color/colorGreen"
android:layout_weight="1"
android:checked="false"
android:text="Circle" />

<RadioButton
android:id="@+id/rb_oblong"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:buttonTint="@color/colorGreen"
android:checked="false"
android:text="Oblong" />
</LinearLayout>

<RadioButton
android:id="@+id/rb_rectangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:buttonTint="@color/colorGreen"
android:checked="false"
android:text="Square/Rectangle" />


</RadioGroup>
</LinearLayout>