1.动态添加2个垂直排列的Button
final LinearLayout layout2=new LinearLayou(this);
定义一个LinearLayout ,参数为context在这儿即为this
layout2.setOrientation(LinearLayout.VERTICAL);
设置layout格式为vertical,竖直排列
2.在Button的click事件中动态添加一个button
与上一例子区别就是,在Button的OnClickListener中动态添加控件,需要注意的一点是 Button btn3=new Button(v.getContext);参数v.getContext为当前view的context,为什么例子1中用this作为context呢,呵呵,这是因为this即当前的activity,而activity又是context的子类,所以this就可以直接作为activity了。