尝试相对布局:
RelativeLayout Main = new RelativeLayout(this);
RelativeLayout.LayoutParams viewParamsCenter = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
Main.setLayoutParams(viewParamsCenter);
Button but = new Button(this);
but.setText("BTN");
but.setBackgroundResource(R.drawable.ic_launcher);
viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
viewParamsCenter.addRule(RelativeLayout.ALIGN_PARENT_TOP);
viewParamsCenter.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
but.setLayoutParams(viewParamsCenter);
Main.addView(but);
but = new Button(this);
but.setText("BTN 2");
but.setBackgroundResource(R.drawable.ic_launcher);
viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
viewParamsCenter.addRule(RelativeLayout.ALIGN_PARENT_TOP);
viewParamsCenter.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
but.setLayoutParams(viewParamsCenter);
Main.addView(but);
but = new Button(this);
but.setText("Center");
but.setId(998900);
but.setBackgroundResource(R.drawable.ic_launcher);
viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
viewParamsCenter.addRule(RelativeLayout.CENTER_IN_PARENT);
but.setLayoutParams(viewParamsCenter);
Main.addView(but);
but = new Button(this);
but.setText("below");
but.setBackgroundResource(R.drawable.ic_launcher);
viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
viewParamsCenter.addRule(RelativeLayout.BELOW, 998900);
viewParamsCenter.addRule(RelativeLayout.CENTER_IN_PARENT);
but.setLayoutParams(viewParamsCenter);
Main.addView(but);
but = new Button(this);
but.setText("Center above");
but.setBackgroundResource(R.drawable.ic_launcher);
viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
viewParamsCenter.addRule(RelativeLayout.ABOVE, 998900);
viewParamsCenter.addRule(RelativeLayout.CENTER_IN_PARENT);
but.setLayoutParams(viewParamsCenter);
Main.addView(but);
but = new Button(this);
but.setText("Center left");
but.setBackgroundResource(R.drawable.ic_launcher);
viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
viewParamsCenter.addRule(RelativeLayout.LEFT_OF, 998900);
viewParamsCenter.addRule(RelativeLayout.CENTER_IN_PARENT);
but.setLayoutParams(viewParamsCenter);
Main.addView(but);
but = new Button(this);
but.setText("Center right");
but.setBackgroundResource(R.drawable.ic_launcher);
viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
viewParamsCenter.addRule(RelativeLayout.RIGHT_OF, 998900);
viewParamsCenter.addRule(RelativeLayout.CENTER_IN_PARENT);
but.setLayoutParams(viewParamsCenter);
Main.addView(but);
but = new Button(this);
but.setText("Bottom");
but.setBackgroundResource(R.drawable.ic_launcher);
viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
viewParamsCenter.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
but.setLayoutParams(viewParamsCenter);
Main.addView(but);
RelativeLayout Main_SUPER = (RelativeLayout) findViewById(R.id.relativeLayout);
Main_SUPER.addView(Main);