问题描述
我有一个ConstraintLayout作为根布局,这很好。
但是我现在有一个RadioGroup,必须在其中创建两列RadioButtons。
因为ConstraintLayout是要摆脱嵌套布局,所以我认为将那些RadioButton放在RadioGroup中并适当地放置它们会很好。
原来有一个ConstraintLayout作为根布局,包含
但是也许我错了。
I have a ConstraintLayout as a root Layout and it's fine.
However I now have a RadioGroup where I have to make two columns of RadioButtons within it.Since ConstraintLayout is about getting the rid of Nested Layouts, I thought it would be fine placing those RadioButtons in the RadioGroup and place them appropriately.
Turns out having a ConstraintLayout as a Root layout, Containing the RadioGroup, doesn't seem to change anything.
But maybe I'm wrong.
你们如何实现在RadioGroup中具有两排RadioButton,
How would you guys achieve having two rows of RadioButtons within a RadioGroup, which is inside a ConstraintLayout?
干杯
推荐答案
视图
必须使用其直接父级的布局属性。例如,您不能将 RadioButton
s与 layout_constraint
s一起使用,因为直接父级是 RadioGroup
和 RadioGroup
不知道如何解释这些属性。
View
s have to use layout attributes of their direct parent. You can't, for instance, have RadioButton
s with layout_constraint
s, because the direct parent is a RadioGroup
and RadioGroup
doesn't know how to interpret those attributes.
RadioGroup
扩展了 LinearLayout
,所以最好使用单个 RadioGroup
是 RadioButton
s的一行或一列。您可能在布局中有两个 RadioGroup
s,并且在您的Java代码中监听了两者的更改。
RadioGroup
extends LinearLayout
, so the best you can do with a single RadioGroup
is a single row or column of RadioButton
s. You could have two RadioGroup
s in your layout and in your java code listen for changes on both.
private RadioGroup mGroup1; // init in onCreate
private RadioGroup mGroup2; // init in onCreate
private OnCheckedChangedListener mCheckListener = new OnCheckedChangedListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// To make it appear as if the two groups are one large group,
// checking something in either should clear the check in the other.
RadioGroup otherGroup = group == mGroup1 ? mGroup2 : mGroup1;
otherGroup.clearCheck();
// do something with checkedId
}
}
这篇关于ConstraintLayout,RadioGroup和RadioButton的两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!