【发布时间】:2011-11-16 22:09:48
【问题描述】:
我正在尝试在按下按钮后创建按钮(这个按钮是用 XML 预制的)。 问题是,我可以在 LinearLayout 中执行此操作,但是当我每次运行 buttoncreator 方法时切换到 RelativeLayout 时,它会删除先前创建的按钮并创建一个新按钮。
更具体; 我的按钮创建方法
public void buttoncreator(String name,RelativeLayout.LayoutParams position,RelativeLayout layout){
positionrandomer(position);
final Button dummybutton = new Button(this);
dummybutton.setText(name);
//these are here for test,it works but still i have the same problem
position.addRule(RelativeLayout.BELOW,R.id.button1);
position.addRule(RelativeLayout.BELOW,R.id.button2);
dummybutton.setLayoutParams(position);
layout.addView(dummybutton);
return;
}
(位置随机器是一种设置随机边距的方法)。
我调用创建者方法的地方
Button luckbutton = (Button) findViewById(R.id.button1);
luckbutton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View arg0) {
name="Blue";
buttoncreator(name,position,layout);
name="Blu4e";
buttoncreator(name,position,layout);
}
});
所以,我的意图是在 1 个按钮单击时创建 2 个按钮,但它只创建 1 个。(实际上,它创建第一个按钮,然后删除它并创建第二个按钮)。
显然,RelativeLayouts 有一些我不明白的地方, 我做错了什么?
提前谢谢
【问题讨论】:
标签: android button onclick android-relativelayout