Как равномерно выделять радиообъективы в Android?

У меня есть три радиообъектива, и я хочу равномерно разместить их по экрану. Когда я использую android:layout_weight="1", кнопки растягиваются по экрану. Итак, как бы у меня было такое же пространство между каждым из них, которое также масштабируется при разных размерах экрана?

<RadioGroup 
        android:id="@+id/auton_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="10dp"
        android:layout_below="@id/clear_fields"
                >
        <RadioButton
            android:id="@+id/auton_radio_1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" 
            android:background="@drawable/auton_col"
            android:layout_weight="1"

            />
        <!-- android:layout_marginRight="380dp"  --> 
        <RadioButton
            android:id="@+id/auton_radio_2"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" 
            android:background="@drawable/auton_col"
            android:layout_weight="1"


            />
        <RadioButton
            android:id="@+id/auton_radio_3"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" 
            android:background="@drawable/auton_col"
            android:layout_weight="1"
            />

    </RadioGroup>

Ответы

Ответ 1

Если вы хотите, чтобы они равномерно распределяли ширину экрана, вам нужно установить android:layout_width="match_parent" для каждого View. Ваш xml станет следующим:

<RadioGroup
    android:id="@+id/auton_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/clear_fields"
    android:orientation="horizontal"
    android:paddingLeft="10dp" >

    <RadioButton
        android:id="@+id/auton_radio_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/auton_col" />
    <!-- android:layout_marginRight="380dp" -->

    <RadioButton
        android:id="@+id/auton_radio_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/auton_col" />

    <RadioButton
        android:id="@+id/auton_radio_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/auton_col" />
</RadioGroup>

Чтобы разработать, layout_weight можно использовать двумя способами.

  • Если у вас несколько видов в вертикальном линейном макете, и вы хотите, чтобы последний занял все оставшееся пространство, вы можете установить их высоту на wrap_content и дать последнему виду вес 1.
  • Если вы хотите, чтобы все виды отображали доступное пространство, установите для всех ширины/высоты значение 0dp или match_parent и дайте каждому виду такое же значение веса. Они будут разделять пространство одинаково.

Чтобы иметь свой фоновой масштабируемый масштаб, создайте новый xml, который находится в вашей папке drawable/, которая выглядит так:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center"
    android:src="@drawable/auton_col" />

Назовите его, как вам нравится (например, auton_col_scale.xml), и ссылку, которую можно использовать в качестве фона.

Ответ 2

Я заметил, что использование макетов весов для RadioButton заставляло их выровняться по центру, хотя они определенно разделяли 50% экрана (они были выровнены по левому краю). Установка силы тяжести для каждого RadioButton вызывала только центрирование текста /facepalm

В приведенном ниже XML показано, как горизонтально выровнять две радиообъекты (могут быть любые виды) и центрировать их:

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

                    <Space
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <RadioButton
                        android:id="@+id/radioyes"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/yes"
                        android:checked="false"/>

                    <Space
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <RadioButton
                        android:id="@+id/radiono"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/no"
                        android:checked="false"/>

                    <Space
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                </RadioGroup>