如何动态创建Android的一个按钮?
问题描述:
可能重复:
如何动态地在Android中添加按钮?
我如何动态地在Android中创建一个按钮?
How can I dynamically create a button in Android?
答
首先添加相应的导入的你的活动:
Firstly add the appropriate import to your Activity:
import android.widget.Button;
然后onCreate方法中创建一个新的按钮对象:
Then create a new button object within the onCreate method:
Button myButton = new Button(this);
myButton.setText("Press Me");
最后,将按钮添加到布局:
Finally add the button to the layout:
LinearLayout layout = (LinearLayout) findViewById(R.id.layout1);
layout.addView(myButton);