How to adjust space between radio button in android

核能气质少年 提交于 2021-02-16 14:55:47

问题


In my android application using radio buttons to select screen view. I want radio button equally aligned horizontal. I tried layout xml shown below.

<?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" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Views"
    android:paddingLeft="10dp"
    android:textSize="20sp"
    />

<RadioGroup
    android:id="@+id/radioView"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:measureWithLargestChild="true"
    android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radioViewSingle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/single"
            android:layout_weight="1"
            android:checked="true" />

        <RadioButton
            android:id="@+id/radioView2by2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableRight="@drawable/view2x2" />

        <RadioButton
            android:id="@+id/radioView3by3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableRight="@drawable/view3x3" />

        <RadioButton
            android:id="@+id/radioView4by4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableRight="@drawable/view4x4" />
</RadioGroup>
</LinearLayout>

output obtained using above xml code

But i expecting below output

Thanks in advance


回答1:


Instead of using android:drawableRight, use android:drawableLeft.

I just tested this and it is working as expected




回答2:


Remove android:layout_weight="1" for all RadioButtons and set android:layout_width="0dp" to android:layout_width="wrap_content"

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:text="Views"
        android:textSize="20sp" />

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

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left">

            <RadioButton
                android:id="@+id/radioViewSingle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:drawableRight="@android:drawable/ic_menu_search" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left">

            <RadioButton
                android:id="@+id/radioView2by2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="25dp"
                android:drawableRight="@android:drawable/ic_menu_camera" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left">

            <RadioButton
                android:id="@+id/radioView3by3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableRight="@android:drawable/ic_menu_always_landscape_portrait" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left">

            <RadioButton
                android:id="@+id/radioView4by4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="25dp"
                android:drawableRight="@android:drawable/ic_menu_call" />
        </LinearLayout>
    </RadioGroup>
</LinearLayout>



回答3:


You can put each RadioButton to one layout and give all layout_width="0dp" and give layout_weight="1" as showen below:

<RadioGroup
    android:id="@+id/radioView"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:measureWithLargestChild="true"
    android:orientation="horizontal">

    <LinearLayout
        android:gravity="left"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_width="0dp">

        <RadioButton
            android:checked="true"
            android:drawableRight="@drawable/single"
            android:id="@+id/radioViewSingle"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:gravity="left"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_width="0dp">

        <RadioButton
            android:drawableRight="@drawable/view2x2"
            android:id="@+id/radioView2by2"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:gravity="left"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_width="0dp">

        <RadioButton
            android:drawableRight="@drawable/view3x3"
            android:id="@+id/radioView3by3"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:gravity="left"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_width="0dp">

        <RadioButton
            android:drawableRight="@drawable/view4x4"
            android:id="@+id/radioView4by4"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
    </LinearLayout>
</RadioGroup>


来源:https://stackoverflow.com/questions/35313174/how-to-adjust-space-between-radio-button-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!