①首先,定义
private LinearLayout baseLayout; //目标控件所在的布局 private Context context; private Button btn1; //即将动态添加的按钮
②生成按钮
btn1 = new Button(context); btn1.setText("动态添加按钮");
③将按钮添加到布局
baseLayout = (LinearLayout)findViewById(R.id.BaseLayout); baseLayout.addView(btn1);