Android uses code to implement RelativeLayout Layout

Source: Internet
Author: User

You only need to copy the code to the oncreate method to run it.

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 is located at the top of the parent View and horizontally centered in the parent View
  9. Rl. addView (btn1, lp1 );
  10. Button btn2 = new Button (this );
  11. Btn2.setText ("| \ 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 is located below btn1 and alignment to the left of btn1
  17. Rl. addView (btn2, lp2 );
  18. Button btn3 = new Button (this );
  19. Btn3.setText ("| \ 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 at the bottom of btn1, right of btn2, and right of btn3 is aligned with the right of btn1 (to be expanded)
  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 horizontally centered in the parent Veiw
  34. Rl. addView (btn4, lp4 );
  35. SetContentView (rl );

 

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.