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
- Private void setlistpath (context, relativelayout footerrelativelayout, string buttontext ){
- If (flagaddfooterguide ){
- // My music buttons
- Imageview mymusic = new imageview (context );
- Mymusic. setimageresource (R. drawable. style#mymusic );
- Mymusic. setonclicklistener (New onclicklistener (){
- @ Override
- Public void onclick (view v ){
- // Todo auto-generated method stub
- (Mymusicactivity) mycontext). setsdcardflag (false );
- (Mymusicactivity) mycontext). oncreate (null );
- }
-
- });
- Mymusic. setid (1); // Set ID to 1
-
- Imageview mystorage = new imageview (context );
- Mystorage. setimageresource (R. drawable. style1_mymusic_usbstore );
- Mystorage. setonclicklistener (New onclicklistener (){
-
- @ Override
- Public void onclick (view v ){
- // Todo auto-generated method stub
- // Set listview to display
- (Mymusicactivity) mycontext). setlistviewlinearlayoutvisible (false );
- // Galleryvisible is hidden
- (Mymusicactivity) mycontext). setgalleryvisible (true );
- }
-
- });
-
- Mystorage. setid (2); // Set ID to 2
- // Set the button for the local current directory
- Myimagebutton myimagebuttonfoldernow = new myimagebutton (context, R. drawable. style+mymusic_details, buttontext );
-
- Relativelayout. layoutparams lp1 = new relativelayout. layoutparams (viewgroup. layoutparams. wrap_content,
- Viewgroup. layoutparams. wrap_content );
- Lp1.addrule (relativelayout. align_parent_left );
-
- Relativelayout. layoutparams lp2 = new relativelayout. layoutparams (viewgroup. layoutparams. wrap_content,
- Viewgroup. layoutparams. wrap_content );
- Lp2.addrule (relativelayout. right_of, 1); // on the right of the control whose ID is 1
-
- Relativelayout. layoutparams lp3 = new relativelayout. layoutparams (viewgroup. layoutparams. wrap_content,
- Viewgroup. layoutparams. wrap_content );
- Lp3.addrule (relativelayout. right_of, 2 );
-
- Footerrelativelayout. addview (mymusic, lp1 );
- Footerrelativelayout. addview (mystorage, lp2 );
-
- // Play button
- Myimagebutton myimagebuttonplay = new myimagebutton (context, R. drawable. mymusic_button_pause_backgroud, "play ");
-
- Footerrelativelayout. addview (myimagebuttonfoldernow, lp3 );
-
- Myimagebuttonplay. setid (4 );
- Relativelayout. layoutparams lp4 = new relativelayout. layoutparams (viewgroup. layoutparams. wrap_content,
- Viewgroup. layoutparams. wrap_content );
-
- Lp4.addrule (relativelayout. left_of, 5 );
-
- // Random button
- Myimagebutton myimagebuttonplayrandom = new myimagebutton (context, R. drawable. mymusic_button_play_random, "random on ");
-
- Myimagebuttonplayrandom. setid (5 );
- Relativelayout. layoutparams lp5 = new relativelayout. layoutparams (viewgroup. layoutparams. wrap_content,
- Viewgroup. layoutparams. wrap_content );
- Lp5.addrule (relativelayout. left_of, 6 );
-
- // Lyrics button
- Myimagebutton myimagebuttonlyric = new myimagebutton (context, R. drawable. mymusic_button_lyric, "LRC lyrics ");
- Myimagebuttonlyric. setid (6 );
- Relativelayout. layoutparams lp6 = new relativelayout. layoutparams (viewgroup. layoutparams. wrap_content,
- Viewgroup. layoutparams. wrap_content );
- Lp6.addrule (relativelayout. align_parent_right );
- // Add
- Footerrelativelayout. addview (myimagebuttonlyric, lp6 );
- Footerrelativelayout. addview (myimagebuttonplayrandom, lp5 );
- Footerrelativelayout. addview (myimagebuttonplay, lp4 );
- Flagaddfooterguide = false;
? [Copy
To clipboard] Download
Zuiniuwang. Java
Another linearlayout
? [Copy
To clipboard] Download
Zuiniuwang. Java
- Super. oncreate (icicle );
- Linearlayout layout = new linearlayout (this );
- Layout. setorientation (linearlayout. Vertical );
-
- BTN = new button (this );
- BTN. setid (101 );
- BTN. settext ("test logoff ");
- BTN. setonclicklistener (this );
- Linearlayout. layoutparams Param =
- New linearlayout. layoutparams (100,50 );
- Param. topmargin = 10;
- Layout. addview (BTN, Param );
-
- Btn2 = new button (this );
- Btn2.setid (102 );
- Btn2.settext ("exit ");
- Btn2.setonclicklistener (this );
- Layout. addview (btn2, Param );
-
- TV = new textview (this );
- TV. settextcolor (color. White );
- TV. settext ("");
- // FP is final int fp = linearlayout. layoutparams. fill_parent;
- // Final int WC = linearlayout. layoutparams. wrap_content;
- Linearlayout. layoutparams param2 =
- New linearlayout. layoutparams (FP, WC );
- Param2.topmargin = 10;
- Param2.setmargins (0, 0, 5, 0); // The parameters are (INT left, int top, int right, int bottom)
- Layout. addview (TV, param2 );
- 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.