只需把代码copy进入oncreate方法即可运行
- RelativeLayout rl = new RelativeLayout(this);
-
- Button btn1 = new Button(this);
- btn1.setText("----------------------");
- btn1.setId(1);
-
- RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
- lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
- lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
-
- rl.addView(btn1, lp1 );
-
- Button btn2 = new Button(this);
- btn2.setText("|\n|\n|\n|\n|\n|");
- btn2.setId(2);
-
- RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
- lp2.addRule(RelativeLayout.BELOW, 1);
- lp2.addRule(RelativeLayout.ALIGN_LEFT, 1);
-
- rl.addView(btn2, lp2);
-
- Button btn3 = new Button(this);
- btn3.setText("|\n|\n|\n|\n|\n|");
- btn3.setId(3);
-
- RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
- lp3.addRule(RelativeLayout.BELOW, 1);
- lp3.addRule(RelativeLayout.RIGHT_OF, 2);
- lp3.addRule(RelativeLayout.ALIGN_RIGHT, 1);
-
- rl.addView(btn3,lp3);
-
- Button btn4 = new Button(this);
- btn4.setText("--------------------------------------------");
- btn4.setId(4);
-
- RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
- lp4.addRule(RelativeLayout.BELOW, 2);
- lp4.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
-
- rl.addView(btn4,lp4);
-
-
- setContentView(rl);
代码参考自:
http://hi.baidu.com/xiechengfa/blog/item/5a3570eb3afb1a31b90e2d54.html, 有改动