Android uses code to implement relativelayout layouts

Source: Internet
Author: User

Simply copy the code into the OnCreate method to run
Java code
  1. Relativelayout RL = New Relativelayout (this);
  2. Button BTN1 = New button (this);
  3. Btn1.settext ("----------------------");
  4. Btn1.setid (1);
  5. Relativelayout.layoutparams LP1 = new Relativelayout.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT,  ViewGroup.LayoutParams.WRAP_CONTENT);
  6. Lp1.addrule (Relativelayout.align_parent_top);
  7. Lp1.addrule (Relativelayout.center_horizontal, relativelayout.true);
  8. //BTN1 at the top of the parent view, centered horizontally in the parent view
  9. Rl.addview (BTN1, LP1);
  10. Button BTN2 = New button (this);
  11. Btn2.settext ("|\n|\n|\n|\n|\n|");
  12. Btn2.setid (2);
  13. Relativelayout.layoutparams LP2 = new Relativelayout.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT,  ViewGroup.LayoutParams.WRAP_CONTENT);
  14. Lp2.addrule (Relativelayout.below, 1);
  15. Lp2.addrule (Relativelayout.align_left, 1);
  16. //BTN2 at the bottom of btn1, to the left and to the left of the BTN1
  17. Rl.addview (BTN2, LP2);
  18. Button BTN3 = New button (this);
  19. Btn3.settext ("|\n|\n|\n|\n|\n|");
  20. Btn3.setid (3);
  21. Relativelayout.layoutparams lp3 = new Relativelayout.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT,  ViewGroup.LayoutParams.WRAP_CONTENT);
  22. Lp3.addrule (Relativelayout.below, 1);
  23. Lp3.addrule (relativelayout.right_of, 2);
  24. Lp3.addrule (Relativelayout.align_right, 1);
  25. //BTN3 is located below the btn1, right side of the BTN2, and aligned with BTN1 's right (to expand)
  26. Rl.addview (BTN3,LP3);
  27. Button BTN4 = New button (this);
  28. Btn4.settext ("--------------------------------------------");
  29. Btn4.setid (4);
  30. Relativelayout.layoutparams LP4 = new Relativelayout.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT,  ViewGroup.LayoutParams.WRAP_CONTENT);
  31. Lp4.addrule (Relativelayout.below, 2);
  32. Lp4.addrule (Relativelayout.center_horizontal, relativelayout.true);
  33. //Btn4 is located below btn2 and is centered horizontally in parent veiw
  34. Rl.addview (BTN4,LP4);
  35. Setcontentview (RL);



Code reference from:
Http://hi.baidu.com/xiechengfa/blog/item/5a3570eb3afb1a31b90e2d54.html, there's a change.

Android uses code for relativelayout layouts

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.