0

Я хочу RadioGroup с различными RadioButtons отображается в двух столбцах. Но кажется, что RadioGroup заставляет RadioButtons отображаться всего в одной колонке. Есть ли способ, чтобы кнопки отображались в двух столбцах?Android RadioGroup. Вынужденные кнопки в одной колонке?

Например, этот xml-код помещает четыре кнопки в square form, но внутри RadioGroup все они находятся в one column.

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

    <RadioButton 
     android:id="@+id/radio1" 
     android:text="Button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <RadioButton 
     android:id="@+id/radio2" 
     android:text="Button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="42dp" 
     android:layout_marginEnd="42dp" 
     android:layout_alignBottom="@+id/radio1"/> 

    <RadioButton 
     android:id="@+id/radio3" 
     android:text="Button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/radio1" 
     android:layout_alignLeft="@+id/radio1" 
     android:layout_alignStart="@+id/radio1" 
     android:layout_marginTop="34dp"/> 

    <RadioButton 
     android:id="@+id/radio4" 
     android:text="Button4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/radio3" 
     android:layout_alignLeft="@+id/radio2" 
     android:layout_alignStart="@+id/radio2"/> 
</RadioGroup> 
+2

RadioGroup расширяет LinearLayout, поэтому просто это невозможно. Вы можете переопределить RelativeLayout и выполнить в нем некоторую логику проверки/отмены. – convexHull

+0

@convexHull И как я могу переопределить его и получить эти кнопки в виде квадратной формы? –

+1

Проверьте это https://developer.android.com/training/custom-views/index.html Создайте некоторый класс (например, RelativeRadioGroup), и пусть он расширяет RelativeLayout. Проверьте исходный код для RadioGroup, чтобы узнать, как реализовать дополнительную логику для проверки/снятия кнопок – convexHull

ответ

1

Отображение радиокнопки в нескольких столбцах с использованием группы радиостанций не может быть выполнено. Однако это может быть иначе, как показано ниже.

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

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

     <RadioButton 
      android:id="@+id/radio1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button1" /> 

     <RadioButton 
      android:id="@+id/radio2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginEnd="42dp" 
      android:layout_marginRight="42dp" 
      android:text="Button2" /> 

    </LinearLayout> 

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

     <RadioButton 
      android:id="@+id/radio3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button3" /> 

     <RadioButton 
      android:id="@+id/radio4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button4" /> 

    </LinearLayout> 
</LinearLayout> 

set View.onClickListener для всех переключателей и настроек ручек проверяет/снимает статус в вашем коде.

Смежные вопросы