我正在尝试为线性布局制作一个动态单选按钮。我有两个从列表调用并添加到线性布局的单选按钮。一切都很好,直到我看不好我的单选按钮并被这个布局卡住了。我的问题是为什么我的单选按钮布局根本不起作用?
这是我的代码:
TextView label;
RadioButton[] rb;
RadioGroup radiogroup;
LinearLayout ll;
String title;
public MyBtn(Context context,String labelText,String options) {
super(context);
ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(params);
label = new TextView(context);
label.setText(labelText);
label.setTextColor(getResources().getColor(R.color.label_color));
radiogroup = new RadioGroup(context);
radiogroup.setOrientation(RadioGroup.HORIZONTAL);
radiogroup.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams [] rbLayout = new RadioGroup.LayoutParams[2];
LinearLayout.LayoutParams rbParam = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
rbParam.weight = 1f;
rbParam.gravity = Gravity.CENTER | Gravity.LEFT;
LinearLayout.LayoutParams rbParam2 = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
rbParam2.gravity = Gravity.CENTER | Gravity.RIGHT;
rbParam2.weight = 1f;
rbLayout[0] = new RadioGroup.LayoutParams(rbParam);
rbLayout[1] = new RadioGroup.LayoutParams(rbParam2);
List<String> list = Arrays.asList(options.split("\\|"));
String[] opts = new String[list.size()];
list.toArray(opts);
rb = new RadioButton[list.size()];
for (int i = 0; i < list.size(); i++) {
rb[i] = new RadioButton(context);
rb[i].setText(opts[i]);
rb[i].setId(i);
radiogroup.addView(rb[i],rbLayout[i]);
}
title = label.getText().toString();
radiogroup.check(0);
ll.addView(label);
ll.addView(radiogroup);
//int resID = getResources().getIdentifier(buttonID, "id", context.getPackageName());
this.addView(ll);
}
谢谢你。