【问题标题】:Android: Radio button alignment 2 x 2 in one radiogroupAndroid:单选按钮对齐 2 x 2 在一个单选组中
【发布时间】:2016-01-29 10:23:33
【问题描述】:

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

A B C D 

A
B
C
D

但我想要的是这种对齐方式:

A B
C D

有没有办法做到这一点?,我找不到任何合适的教程或示例。

【问题讨论】:

标签: android radio-button android-radiobutton


【解决方案1】:

像这样在 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>

【讨论】:

  • 它工作谢谢!,我尝试做 gridview 但我认为这更容易。 tnx
  • 问题是我现在无法得到 radiobtn 的值
  • 使用单独的单选按钮,然后以编程方式控制那里的开/关状态。
  • 我只能得到一个radiobtn ID int radioButtonId = r1.getId(); RadioButton selectedans = (RadioButton) findViewById(radioButtonId); String selectedansText = selectedans.getText().toString();
  • 我这样做了但不能只选择一个单选按钮..它允许选择多个违反单选按钮的单选按钮
【解决方案2】:

RadioGroup 扩展了 android.widget.LinearLayout 所以这是不可能的。 如果您希望它们在网格中使用适当的布局,但您必须自己完成仅选择一项等整个逻辑

【讨论】:

    【解决方案3】:

    您使用 GridLayout 可以提供帮助。

    【讨论】:

    • 先生,您能详细说明一下吗?或示例
    【解决方案4】:

    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

    【讨论】:

      猜你喜欢
      • 2017-01-16
      • 2011-05-12
      • 2015-01-11
      • 2015-12-27
      • 1970-01-01
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      • 2017-06-23
      相关资源
      最近更新 更多