Android 动态添加Button(1)
这是一个非常简单的动态添加Button的例子:
package com.kkkk.llll;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
public class AddButton extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
final LinearLayout layout2 = new LinearLayout(this);
layout2.setOrientation(LinearLayout.VERTICAL);
Button bt1 = new Button(this);
setContentView(layout2);
Button bt2 = new Button(this);
bt1.setText("按钮1");
bt2.setText("按钮2");
layout2.addView(bt1);
layout2.addView(bt2);
setContentView(layout2);
}
}
XML文件里面不需要进行任何配置。