Радиокнопка в горизонтальном и вертикальном направлении в одной радиогруппе

мне удалось поместить радиокнопки в горизонтальном и вертикальном направлении, но они находятся в двух радиогруппах, но я хочу, чтобы все радиокнопки были помещены в одну радиогруппу.

Мой вывод выглядит нормально Но они находятся в двух радиогруппах, как я достигаю этого типа компоновки с одной радиогруппой.

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
tools:context="com.example.asad.homebuyerproject.ResidentialFragment">


<RadioGroup
    android:id="@+id/ReplaceAreaCommercial"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="#FFFFFF"
    android:layout_marginLeft="10dp">


    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/hostdrawable" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Residential"
        android:button="@drawable/houdrawable" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Residential"
        android:button="@drawable/uilfloor" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Residential"
        android:button="@drawable/penhoudrawable" />


</RadioGroup>


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="160dp"
    android:layout_marginStart="160dp"

    >

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/fladrawable" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/vildrawable" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:elevation="0dp"
            android:button="@drawable/studaprtment" />


    </RadioGroup>

</RelativeLayout>


person Shehzad Khan    schedule 17.11.2016    source источник
comment
Вы должны использовать Gridview или Recyclerview с опцией Gridlayout.   -  person android_griezmann    schedule 17.11.2016
comment
@android_griezmann любой пример, пожалуйста   -  person Shehzad Khan    schedule 17.11.2016
comment
Возможное решение с gridview здесь   -  person android_griezmann    schedule 17.11.2016


Ответы (1)


RadioGroup расширяет LinearLayout, поэтому невозможно выровнять RadioButton в сетке ниже, это решение, которое я реализовал из другого ответа, взгляните на это

package com.devprovider.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.TableLayout;
import android.widget.TableRow;


public class ToggleButtonGroupTableLayout extends TableLayout  implements OnClickListener {

    private static final String TAG = "ToggleButtonGroupTableLayout";
    private RadioButton activeRadioButton;

    /** 
     * @param context
     */
    public ToggleButtonGroupTableLayout(Context context) {
        super(context);

    }

    /**
     * @param context
     * @param attrs
     */
    public ToggleButtonGroupTableLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    @Override
    public void onClick(View v) {
        final RadioButton rb = (RadioButton) v;
        if ( activeRadioButton != null ) {
            activeRadioButton.setChecked(false);
        }
        rb.setChecked(true);
        activeRadioButton = rb;
    }


    @Override
    public void addView(View child, int index,
            android.view.ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        setChildrenOnClickListener((TableRow)child);
    }



    @Override
    public void addView(View child, android.view.ViewGroup.LayoutParams params) {
        super.addView(child, params);
        setChildrenOnClickListener((TableRow)child);
    }


    private void setChildrenOnClickListener(TableRow tr) {
        final int c = tr.getChildCount();
        for (int i=0; i < c; i++) {
            final View v = tr.getChildAt(i);
            if ( v instanceof RadioButton ) {
                v.setOnClickListener(this);
            }
        }
    }

    public int getCheckedRadioButtonId() {
        if ( activeRadioButton != null ) {
            return activeRadioButton.getId();
        }

        return -1;
    }
}

ваш макет будет таким

<?xml version="1.0" encoding="utf-8"?>
<com.devprovider.customview.ToggleButtonGroupTableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:id="@+id/radGroup1">
    <TableRow>
            <RadioButton android:id="@+id/rad1" android:text="Button1"
                android:layout_width="105px" android:layout_height="wrap_content"
                android:textSize="13px" />
            <RadioButton android:id="@+id/rad2" android:text="Button2"
                android:layout_width="105px" android:textSize="13px"
                android:layout_height="wrap_content" />

    </TableRow>
    <TableRow>
            <RadioButton android:id="@+id/rad1" android:text="Button1"
                android:layout_width="105px" android:layout_height="wrap_content"
                android:textSize="13px" />
            <RadioButton android:id="@+id/rad2" android:text="Button2"
                android:layout_width="105px" android:textSize="13px"
                android:layout_height="wrap_content" />

    </TableRow>
    <TableRow>
            <RadioButton android:id="@+id/rad1" android:text="Button1"
                android:layout_width="105px" android:layout_height="wrap_content"
                android:textSize="13px" />
            <RadioButton android:id="@+id/rad2" android:text="Button2"
                android:layout_width="105px" android:textSize="13px"
                android:layout_height="wrap_content" />

    </TableRow>
</com.devprovider.customview.ToggleButtonGroupTableLayout>

Благодаря этому ответу кнопка Radio Button, такая как GridView

person rana_sadam    schedule 17.11.2016