我正在创建RadioButtons并将其动态添加到RadioGroup。但是,当我运行该应用程序时,RadioButton的文本未显示在屏幕上。
这是我的RadioButtons代码
else if ((items.get(i).toString()).equals("rad")) {
RadioGroup bg = new RadioGroup(getApplicationContext());
int child=0;
for (int h = textlen; h < text.size(); textlen++) {
if (text.get(textlen).contains("(")) {
s = text.get(textlen).replace("(", "");
if (s.contains(")"))
s = s.replace(")", "");
} else if (text.get(textlen).contains(")")) {
s = text.get(textlen).replace(")", "");
} else
s = text.get(textlen);
RadioButton radioButton = new RadioButton(
getApplicationContext());
bg.addView(radioButton);
// / radioButton.setName("rbt");
if (s.contains("{on}")) {
// radioButton.setSelected(true);
radioButton.setChecked(true);
s = s.replace("{on}", "");
} else {
radioButton.setChecked(false);
s = s.replace("{of}", "");
}
//((RadioButton)bg.getChildAt(child)).setText(s);
//child++;
radioButton.setText(s);
String c = text.get(textlen).substring(
text.get(textlen).length() - 1);
if (c.equals(")")) {
textlen++;
break;
}
}
layout.addView(bg);
}
当我调试代码时,我可以看到该文本已添加到RadioGropus子级中
但是在运行应用程序时,文本不可见。任何人都可以检测到该问题。多谢
只需添加
setLayoutParams(params);
用于RadioButton以及RadioGroup。
在哪里
android.widget.LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句