tv = new TextView[10];
RelativeLayout rl = (RelativeLayout) findViewById(R.id.RL);
RelativeLayout.LayoutParams lpFirst = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lpFirst.addRule(RelativeLayout.CENTER_HORIZONTAL);
lpFirst.addRule(RelativeLayout.CENTER_VERTICAL);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
for(int i = 0; i < 10; i++){
tv[i] = new TextView(this);
tv[i].setId(i + 1000);
tv[i].setText("" + tv[i].getId());
}
rl.addView(tv[0], lpFirst);
for(int i = 1; i < 10; i++){
lp.addRule(RelativeLayout.RIGHT_OF, tv[i-1].getId());
tv[i].setLayoutParams(lp);
rl.addView(tv[i], lp);
}
这是我的代码 我想电视[1] ~TV [9]放在tv [0]的右侧但是它们放在左上方。 我不知道是什么问题。
答案 0 :(得分:0)
只需将LinearLayout放在XML
中的容器RelativeLayout中head
并按ID获取该容器,并将每个子项添加到其中