代码之家  ›  专栏  ›  技术社区  ›  Drenyl

Android:单选按钮对齐2 x 2在一个单选组中

  •  5
  • Drenyl  · 技术社区  · 9 年前

    我有4个 radio buttons 我只能这样垂直和水平对齐:

    A B C D 
    

    A
    B
    C
    D
    

    但我想要的是这种对齐:

    A B
    C D
    

    有什么可能的方法可以做到这一点吗?,我找不到任何正确的教程或示例。

    4 回复  |  直到 9 年前
        1
  •  5
  •   Gagan Sethi    9 年前

    在Radiogroup中使用LinearLayout,如下所示:

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/radioGroup">
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <RadioButton
                android:id="@+id/radioButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New RadioButton"
    
                android:layout_marginLeft="5dp"/>
    
            <RadioButton
                android:id="@+id/radioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New RadioButton"
    
                android:layout_marginRight="5dp"    />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <RadioButton
                android:id="@+id/radioButton4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New RadioButton"
    
                android:layout_marginLeft="5dp"/>
    
            <RadioButton
                android:id="@+id/radioButton3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New RadioButton"
    
                android:layout_marginRight="5dp"/>
        </LinearLayout>
    </RadioGroup>
    
        2
  •  1
  •   and_dev    9 年前

    RadioGroup扩展了android.widget。LinearLayout,因此不可能。 如果你想在一个网格中使用合适的布局,但你必须自己完成只选择一个项目的整个逻辑,以此类推

        3
  •  1
  •   Phuoc Huynh    9 年前

    您可以使用GridLayout。

        4
  •  1
  •   Community T.Woody    7 年前

    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>
    

    感谢这个答案 Grid of Radio Button