2011-05-05 161 views
-1

我想在某些位置动态添加一些按钮,任何人都可以为此提供示例代码?动态添加按钮?

+2

哦,我的朋友!请阅读http://stackoverflow.com/questions/how-to-ask – 2011-05-05 07:06:05

+0

你想在哪里动态添加按钮? – Pasha 2011-05-05 07:06:32

回答

1

这就是你可以在运行时添加一个按钮,一组:线性布局

final ViewGroup group = (ViewGroup)findViewById(R.id.some_group); 
final Button button = new Button(context); 
button.setText("Some text"); 
group.addView(button, 
    new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
-1
setContentView(R.layout.medplan_index); 

采取定向垂直。

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout); 

Button btn = new Button (this); 
btn.setText("button name"); 
btn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 
linearLayout.addView(btn);