【发布时间】:2013-02-05 02:30:55
【问题描述】:
我对 android 非常陌生,并尝试使用不带 xml 的 android api 水平放置元素。
我想要做的是水平放置 RadioButtons 和 EditText。比如:
R-----E
R-----E
我试过这样的代码:
RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.VERTICAL);//or RadioGroup.VERTICAL
for(Map.Entry<String,String> entry : storedCards.entrySet())
{
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
EditText ed = new EditText(this);
RadioButton rb = new RadioButton(this);
rb.setId(counter++);
lp2.addRule(RelativeLayout.RIGHT_OF,rb.getId());
ed.setLayoutParams(lp2);
rg.addView(rb); //the RadioButtons are added to the radioGroup instead of the layout
rb.setText(entry.getKey());
relativeLayout.addView(ed);
}
这不起作用。但这就是我想要做的:首先,我使用counter 变量为每个单选按钮设置id,并尝试在该单选按钮的右侧站点上设置edittext视图:
lp2.addRule(RelativeLayout.RIGHT_OF,rb.getId());
但我没有得到正确的结果。我得到的只是这样:
ER
R
所有EditText 都被重叠了。我在哪里犯错了?
提前致谢。
【问题讨论】: