我已经做了一些研究,但我发现的答案对我无效。这是我的代码的一部分。该R.id.relative是在XML中的RelativeLayout的ID文件动态地将项目放在RelativeLayout中
RelativeLayout RL = (RelativeLayout) findViewById(R.id.relative);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView title = new TextView(this);
title.setText(" History ");
title.setId(99099);
title.setTextSize(30);
title.setGravity(Gravity.CENTER);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
title.setLayoutParams(params);
RL.addView(title);
RelativeLayout.LayoutParams test_params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
Button test = new Button(this);
test.setText(" Back ");
test_params.addRule(RelativeLayout.BELOW,99099);
test.setId(199291);
test.setGravity(Gravity.CENTER);
test.setLayoutParams(test_params);
RL.addView(test);
RelativeLayout.LayoutParams test_params2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
Button test2 = new Button(this);
test2.setText(" Clear ");
test_params2.addRule(RelativeLayout.BELOW,test.getId());
test2.setGravity(Gravity.CENTER);
test.setLayoutParams(test_params2);
RL.addView(test2);
所有3个项目都出现了,但他们堆叠在一起。我无法让他们低于另一个。 任何人都可以帮忙吗?
你认为Android忽略你的规则 - 'addRule(RelativeLayout.BELOW,title.getId());'? – Axarydax 2013-05-11 05:28:21
将title.getId()更改为99099的id值时会发生什么? – Axarydax 2013-05-11 05:29:28
雅仍然是相同的,现在我试图将参数放入addview方法。 – user2372160 2013-05-11 07:57:58