Мне нужно выровнять RadioButtons внутри RadioGroup, как RelativeLayout. Я читал, что RadioGroup унаследован от LinearLayout, и, возможно, я не могу выровнять содержимое, такое как RelativeLayout внутри него. Фактически я пытаюсь реализовать две строки внутри RadioGroup. Первая строка содержит два RadioButton, а во второй строке я должен добавить еще одну кнопку в ее начале. Как я мог это сделать?
Как я могу выровнять радиокнопки внутри радиогруппы Android?
Ответы (3)
Все, что вам нужно сделать, это установить ориентацию в радиогруппе по горизонтали, чтобы выровнять их по горизонтали, см. Код ниже.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">
<RadioGroup
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton2" />
</RadioGroup>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:layout_gravity="start"
android:id="@+id/radioButton3" />
</LinearLayout>
Можете ли вы попробовать этот макет и сообщить, работает ли он для вас. Я могу изменить это, если потребуется.
<?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="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red"/>
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green"/>
</LinearLayout>
</RadioGroup>
</LinearLayout>
По умолчанию RadioGroup
распознает только RadioButton
s, добавленные как прямые дочерние элементы, и действует как LinearLayout
, но мы можем настроить поведение, если напишем свои собственные RadioGroup
.
Сейчас существует довольно много проектов, поэтому нам не нужно изобретать заново: https://github.com/search?q=radiogroup. Это ссылка на результат поиска, и она не будет недействительной, если Github не выйдет из строя.
Я предпочитаю это решение, оно просто определяет RadioButton
s как косвенные дочерние элементы.