问题
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.
How would you guys achieve having two rows of RadioButtons within a RadioGroup, which is inside a ConstraintLayout?
Cheers
回答1:
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
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
}
}
回答2:
I came up with a simple class that makes it really easy. It's all in a single java file. It's an "unbound" radio group. You can layout your radio buttons however you like, add the buttons to the group, and it acts just like a radioGroup.
The code is at this gist UnboundRadioGroup, with a full explanation but here's the usage for it:
If you don't want to use an anonymous inner class, you can use implements
public class MainActivity extends AppCompatActivity implements UnboundRadioGroup.OnClickListener
There are a few ways to create a group.
Find the root Viewgroup
ViewGroup viewGroup = (ViewGroup) findViewById(android.R.id.content);
// create a radio group with the root viewgroup
UnboundRadioGroup unboundRadioGroup1 = new UnboundRadioGroup(this, viewGroup);
// don't forget to set a click listener for the group. Using implements in this case
unboundRadioGroup1.setOnClickListener(this);
or create a radio group with the id of the root viewgroup, whichever you prefer.
UnboundRadioGroup unboundRadioGroup2 = new UnboundRadioGroup(this, android.R.id.content);
// add your click listener using an inner class
unboundRadioGroup2.setOnClickListener(new UnboundRadioGroup.OnClickListener()
{
@Override
public void OnClick(RadioButton radioButton)
{
Log.i("radioButton", radioButton.getTag().toString());
}
});
This method manually adds buttons to your group
unboundRadioGroup1.add((RadioButton) findViewById(R.id.radioButton1));
unboundRadioGroup1.add((RadioButton) findViewById(R.id.radioButton2));
and this method automatically adds buttons to your group based on the android:tag property in the XML. Note that you should NOT use this method if you need the tags elsewhere in your code. However, if you are not going to need the tags, you can set the tags of multiple radio buttons to the same name, and then this method will create a group from them
unboundRadioGroup2.createGroupByTag("tag");
If you're using implements instead of inner class, your Onclick would be set like this:
@Override
public void OnClick(RadioButton radioButton)
{
Log.i("radioButton", radioButton.getTag().toString());
}
回答3:
Please visit https://github.com/samlu/ConstraintRadioGroup A RadioGroup widget that can be used with ConstraintLayout
The blRadioGroup
widget should be what you are looking for.
来源:https://stackoverflow.com/questions/40499344/constraintlayout-radiogroup-and-two-columns-of-radiobutton