In my project I want to add buttons and radio buttons dynamically. They should be placed side by side. For instance,开发者_开发百科 if I had 10 buttons and 10 radiobuttons, I should get 2 buutons and 2 radiobuttons in each row. How can this be done?
for example if you want to add 7 buttons and radiobuttons to your LinearLayout you can use something like this
private void createRadioButton() {
final RadioButton[] radioButton = new RadioButton[7];
RadioGroup radioGroup = new RadioGroup(this);
radioGroup.setOrientation(RadioGroup.VERTICAL);
for(int i = 0; i < 7; ++i) {
radioButton[i] = new RadioButton(this);
Button buttonView = new Button(this);
buttonView.setText("Button " + i);
radioGroup.addView(radioButton[i]);
radioGroup.addView(buttonView);
radioButton[i].setText("Test" + i);
}
line.addView(radioGroup);
}
精彩评论