Simply copy the code into the OnCreate method to run
Java code
- 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);
- //BTN1 at the top of the parent view, centered horizontally in the parent view
- 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);
- //BTN2 at the bottom of btn1, to the left and to the left of the BTN1
- 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);
- //BTN3 is located below the btn1, right side of the BTN2, and aligned with BTN1 's right (to expand)
- 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);
- //Btn4 is located below btn2 and is centered horizontally in parent veiw
- Rl.addview (BTN4,LP4);
- Setcontentview (RL);
Code reference from:
Http://hi.baidu.com/xiechengfa/blog/item/5a3570eb3afb1a31b90e2d54.html, there's a change.
Android uses code for relativelayout layouts