【发布时间】: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
我有 4 个radio buttons,我只能像这样垂直和水平对齐它们:
A B C D
和
A
B
C
D
但我想要的是这种对齐方式:
A B
C D
有没有办法做到这一点?,我找不到任何合适的教程或示例。
【问题讨论】:
标签: android radio-button android-radiobutton
像这样在 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>
【讨论】:
int radioButtonId = r1.getId(); RadioButton selectedans = (RadioButton) findViewById(radioButtonId); String selectedansText = selectedans.getText().toString();
RadioGroup 扩展了 android.widget.LinearLayout 所以这是不可能的。 如果您希望它们在网格中使用适当的布局,但您必须自己完成仅选择一项等整个逻辑
【讨论】:
您使用 GridLayout 可以提供帮助。
【讨论】:
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
【讨论】: