我正在尝试创建一种方法,每次单击一个按钮时,我希望屏幕在当前存在的一个下面显示一个相对布局框,或者如果没有一个那么在顶部创建一个并且我想要它从每个按钮点击下面继续创建它们。目前使用此方法,单击按钮时没有任何反应。
这是我到目前为止的代码,有人可以帮助我看看我做错了什么以及我可以做些什么来解决这个问题:
public void createNewLayout(){
int currentId = 10;
for(int i = 1; i <= numberOfLayouts; i++){
newLayoutContainer = new RelativeLayout(this);
newLayoutContainer.setId(currentId);
if(currentId == 10){
newLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
newLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, containerLayout2.getId());
newLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT, containerLayout2.getId());
newLayoutParams.addRule(RelativeLayout.BELOW, containerLayout2.getId());
newLayoutParams.setMargins(0, 0, 0, dpMargin);
newLayoutContainer.setBackgroundResource(R.color.display_panels);
}
else{
newLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
newLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, newLayoutContainer.getId());
newLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT, newLayoutContainer.getId());
newLayoutParams.addRule(RelativeLayout.BELOW, currentId-1);
newLayoutParams.setMargins(0, 0, 0, dpMargin);
newLayoutContainer.setBackgroundResource(R.color.display_panels);
}
newLayoutContainer.setLayoutParams(newLayoutParams);
layout.addView(newLayoutContainer);
currentId++;
}
}