Android Dynamic Layout

Source: Internet
Author: User

Original: http://zuiniuwang.blog.51cto.com/3709988/720070

 

Every time I forget it, write it down and find it easily. The code is copied from the current project. First, let's get a relativelayout

 
 
  1. Private void setlistpath (context, relativelayout footerrelativelayout, string buttontext ){
  2. If (flagaddfooterguide ){
  3. // My music buttons
  4. Imageview mymusic = new imageview (context );
  5. Mymusic. setimageresource (R. drawable. style#mymusic );
  6. Mymusic. setonclicklistener (New onclicklistener (){
  7. @ Override
  8. Public void onclick (view v ){
  9. // Todo auto-generated method stub
  10. (Mymusicactivity) mycontext). setsdcardflag (false );
  11. (Mymusicactivity) mycontext). oncreate (null );
  12. }
  13. });
  14. Mymusic. setid (1); // Set ID to 1
  15. Imageview mystorage = new imageview (context );
  16. Mystorage. setimageresource (R. drawable. style1_mymusic_usbstore );
  17. Mystorage. setonclicklistener (New onclicklistener (){
  18. @ Override
  19. Public void onclick (view v ){
  20. // Todo auto-generated method stub
  21. // Set listview to display
  22. (Mymusicactivity) mycontext). setlistviewlinearlayoutvisible (false );
  23. // Galleryvisible is hidden
  24. (Mymusicactivity) mycontext). setgalleryvisible (true );
  25. }
  26. });
  27. Mystorage. setid (2); // Set ID to 2
  28. // Set the button for the local current directory
  29. Myimagebutton myimagebuttonfoldernow = new myimagebutton (context, R. drawable. style+mymusic_details, buttontext );
  30. Relativelayout. layoutparams lp1 = new relativelayout. layoutparams (viewgroup. layoutparams. wrap_content,
  31. Viewgroup. layoutparams. wrap_content );
  32. Lp1.addrule (relativelayout. align_parent_left );
  33. Relativelayout. layoutparams lp2 = new relativelayout. layoutparams (viewgroup. layoutparams. wrap_content,
  34. Viewgroup. layoutparams. wrap_content );
  35. Lp2.addrule (relativelayout. right_of, 1); // on the right of the control whose ID is 1
  36. Relativelayout. layoutparams lp3 = new relativelayout. layoutparams (viewgroup. layoutparams. wrap_content,
  37. Viewgroup. layoutparams. wrap_content );
  38. Lp3.addrule (relativelayout. right_of, 2 );
  39. Footerrelativelayout. addview (mymusic, lp1 );
  40. Footerrelativelayout. addview (mystorage, lp2 );
  41. // Play button
  42. Myimagebutton myimagebuttonplay = new myimagebutton (context, R. drawable. mymusic_button_pause_backgroud, "play ");
  43. Footerrelativelayout. addview (myimagebuttonfoldernow, lp3 );
  44. Myimagebuttonplay. setid (4 );
  45. Relativelayout. layoutparams lp4 = new relativelayout. layoutparams (viewgroup. layoutparams. wrap_content,
  46. Viewgroup. layoutparams. wrap_content );
  47. Lp4.addrule (relativelayout. left_of, 5 );
  48. // Random button
  49. Myimagebutton myimagebuttonplayrandom = new myimagebutton (context, R. drawable. mymusic_button_play_random, "random on ");
  50. Myimagebuttonplayrandom. setid (5 );
  51. Relativelayout. layoutparams lp5 = new relativelayout. layoutparams (viewgroup. layoutparams. wrap_content,
  52. Viewgroup. layoutparams. wrap_content );
  53. Lp5.addrule (relativelayout. left_of, 6 );
  54. // Lyrics button
  55. Myimagebutton myimagebuttonlyric = new myimagebutton (context, R. drawable. mymusic_button_lyric, "LRC lyrics ");
  56. Myimagebuttonlyric. setid (6 );
  57. Relativelayout. layoutparams lp6 = new relativelayout. layoutparams (viewgroup. layoutparams. wrap_content,
  58. Viewgroup. layoutparams. wrap_content );
  59. Lp6.addrule (relativelayout. align_parent_right );
  60. // Add
  61. Footerrelativelayout. addview (myimagebuttonlyric, lp6 );
  62. Footerrelativelayout. addview (myimagebuttonplayrandom, lp5 );
  63. Footerrelativelayout. addview (myimagebuttonplay, lp4 );
  64. Flagaddfooterguide = false;

 

? [Copy
To clipboard] Download
Zuiniuwang. Java

Another linearlayout

? [Copy
To clipboard] Download
Zuiniuwang. Java

 
 
  1. Super. oncreate (icicle );
  2. Linearlayout layout = new linearlayout (this );
  3. Layout. setorientation (linearlayout. Vertical );
  4. BTN = new button (this );
  5. BTN. setid (101 );
  6. BTN. settext ("test logoff ");
  7. BTN. setonclicklistener (this );
  8. Linearlayout. layoutparams Param =
  9. New linearlayout. layoutparams (100,50 );
  10. Param. topmargin = 10;
  11. Layout. addview (BTN, Param );
  12. Btn2 = new button (this );
  13. Btn2.setid (102 );
  14. Btn2.settext ("exit ");
  15. Btn2.setonclicklistener (this );
  16. Layout. addview (btn2, Param );
  17. TV = new textview (this );
  18. TV. settextcolor (color. White );
  19. TV. settext ("");
  20. // FP is final int fp = linearlayout. layoutparams. fill_parent;
  21. // Final int WC = linearlayout. layoutparams. wrap_content;
  22. Linearlayout. layoutparams param2 =
  23. New linearlayout. layoutparams (FP, WC );
  24. Param2.topmargin = 10;
  25. Param2.setmargins (0, 0, 5, 0); // The parameters are (INT left, int top, int right, int bottom)
  26. Layout. addview (TV, param2 );
  27. Setcontentview (layout );
Remember that the created parameter addview must be the sub-control parameter, not the container parameter. This is very important and I have wasted a lot of time here. If you need to add parameters to this layout, you can use layout. setlayoutparams (xxx Params) and layout. setxxx.
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.