import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private LinearLayout ll_parent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获得父控件
ll_parent = (LinearLayout) findViewById(R.id.ll_parent);
// 新建新的LinearLayout控件
LinearLayout ll_up = new LinearLayout(this);
// 新建LinearLayout属性对象
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, 1);
// 添加属性
ll_up.setLayoutParams(params);
// 设置方向
ll_up.setOrientation(LinearLayout.HORIZONTAL);
// 添加图片资源
ll_up.setBackgroundResource(R.drawable.fuck);
// 添加按钮1
// 新建一个按钮
Button btn_01 = new Button(this);
// 设置按钮的字体
btn_01.setText("按钮1");
// 新建按钮的属性
LayoutParams params2 = new LayoutParams(150, LayoutParams.MATCH_PARENT);
// 把属性添加到按钮中
btn_01.setLayoutParams(params2);
// 把按钮添加到父控件中
ll_up.addView(btn_01);
// 添加按钮
Button btn_02 = new Button(this);
btn_02.setText("按钮2");
btn_02.setLayoutParams(params2);
ll_up.addView(btn_02);
// 添加按钮
Button btn_03 = new Button(this);
btn_03.setText("按钮3");
btn_03.setLayoutParams(params2);
ll_up.addView(btn_03);
// 父控件中添加这个控件
ll_parent.addView(ll_up);
LinearLayout ll_down = new LinearLayout(this);
ll_down.setLayoutParams(params);
ll_down.setBackgroundResource(R.drawable.lj0601bz11);
ll_down.setOrientation(LinearLayout.VERTICAL);
// 添加按钮
Button btn_04 = new Button(this);
btn_04.setText("按钮4");
// 这次用的是父类型的类型
LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1);
btn_04.setLayoutParams(params4);
ll_down.addView(btn_04);
// 添加按钮
Button btn_05 = new Button(this);
btn_05.setText("按钮5");
btn_05.setLayoutParams(params4);
ll_down.addView(btn_05);
// 添加按钮
Button btn_06 = new Button(this);
btn_06.setText("按钮6");
btn_06.setLayoutParams(params4);
ll_down.addView(btn_06);
ll_parent.addView(ll_down);
}
}