My RelativeLayout动态添加复选框并为每个复选框设置文本,但即使我使用RelativeLayout.BELOW参数,它们也会相互重叠。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bible_progress);
Bundle progressBundle = getIntent().getExtras();
int positionBook = progressBundle.getInt("position");
if(positionBook == 0) {
for (int i = 0; i < 10; i++) {
RelativeLayout progressLayout = (RelativeLayout) findViewById(R.id.progress_layout);
CheckBox progressBox = new CheckBox(this);
progressBox.setText("Dynamic Checkbox " + i);
progressBox.setId(i + 10);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, progressBox.getId());
progressBox.setLayoutParams(params);
progressLayout.addView(progressBox);
}
}
}
我没有收到任何错误,因此没有堆栈跟踪。
答案 0 :(得分:2)
您为规则提供了错误的ID
params.addRule(RelativeLayout.BELOW, progressBox.getId());
这样您就可以向规则
提供刚刚创建的视图的ID