Выровняйте 3 RadioButtons в RadioGroup по горизонтали как слева, по центру, справа в Android - PullRequest
0 голосов
/ 28 апреля 2020

Как выровнять 3 переключателя (с размерами wrap_content) в группе радио (с размерами match_parent) по горизонтали

Ответы [ 2 ]

0 голосов
/ 28 апреля 2020

Просто задайте ориентацию в Радиогруппе по горизонтали

    <RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Radio button 1"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Radio button 2"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Radio button 3"/>
</RadioGroup>
0 голосов
/ 28 апреля 2020
  • Установите ширину радиогруппы равной match_parent или в dip любой ширине, которую вы хотите
  • Добавить вид (фиктивный вид) между двумя переключателями, то есть между 1-м и 2-м и между 2-м и 3-м, с android: layout_weight = "1"

            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_marginTop="50dp"
                android:gravity="center"
                android:orientation="horizontal"
                android:background="@drawable/shape_bg_view_common"
                app:layout_constraintTop_toBottomOf="@+id/edtSearch">
    
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:background="@null"
                    android:button="@drawable/slctr_all"/>
                <Space
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"/>
    
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@null"
                    android:layout_gravity="center"
                    android:button="@drawable/slctr_many"/>
    
                <Space
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"/>
    
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dp"
                    android:background="@null"
                    android:button="@drawable/slctr_few"/>
            </RadioGroup>
    
  • Пробел также может использоваться вместо View

       <Space
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...