We can usually set the margin directly in XML, such as:
<imageview android:layout_margin= "5dip" android:src= "@drawable/image"/>
However, in some cases, it is necessary to write in Java code, but the view itself does not have a setmargin method, how to do?
By looking at the Android API, we found that Android.view.ViewGroup.MarginLayoutParams has a method SetMargins (left, top, right, bottom).
Its direct subclasses are: Framelayout.layoutparams, Linearlayout.layoutparams and Relativelayout.layoutparams.
How to use:
Linearlayout.layoutparams LP = new Linearlayout.layoutparams (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
Lp.setmargins (10, 20, 30, 40);
IMAGEVIEW.SETLAYOUTPARAMS (LP);
Original: http://greatverve.cnblogs.com/archive/2012/01/29/android-margin.html
Android uses code to implement relativelayout layouts
Relativelayout RL =NewRelativelayout ( This); Button BTN1=NewButton ( This); Btn1.settext ("----------------------"); Btn1.setid (1); Relativelayout.layoutparams LP1=Newrelativelayout.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 viewRl.addview (BTN1, LP1); Button btn2=NewButton ( This); Btn2.settext ("|\n|\n|\n|\n|\n|"); Btn2.setid (2); Relativelayout.layoutparams LP2=Newrelativelayout.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); Lp2.addrule (Relativelayout.below,1); Lp2.addrule (Relativelayout.align_left,1); //BTN2 is located below the BTN1, to the left of the btn1 and to the left of theRl.addview (BTN2, LP2); Button Btn3=NewButton ( This); Btn3.settext ("|\n|\n|\n|\n|\n|"); Btn3.setid (3); Relativelayout.layoutparams LP3=Newrelativelayout.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 BTN2 and aligned to its right side of the BTN1 (to expand)Rl.addview (BTN3,LP3); Button Btn4=NewButton ( This); Btn4.settext ("--------------------------------------------"); Btn4.setid (4); Relativelayout.layoutparams LP4=Newrelativelayout.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 the btn2 and is centered horizontally in the parent veiwRl.addview (BTN4,LP4); Setcontentview (RL);
Original: http://kukuqiu.iteye.com/blog/1018396
Dynamically modify the width of the relativelayout:
Parameters can be. property settings, but values are pixels and need to be converted to DP units.
Relativelayout.layoutparams linearparams = (relativelayout.layoutparams) Rela_addnote_ Notetype.getlayoutparams (); =; Rela_addnote_notetype.setlayoutparams (linearparams);
Original: http://blog.csdn.net/chenguang79/article/details/37874793
Android adaptation requires the knowledge point layoutparams:
Http://www.android100.org/html/201406/05/18971.html
Setting the margin in Java code