Http://www.2cto.com/kf/201207/140111.html
Red and Black Alliance:
Used to set the margin directly in the XML (the distance is around 10dip), such as:
<imageview android:layout_margin= "10dip" android:src= "@drawable/image"/>
In some cases, it is necessary to write in Java code.
API, Android.view.ViewGroup.MarginLayoutParams has a method SetMargins (left, top, right, bottom).
But the view itself does not have the SetMargin method, how to do?
See the API, its immediate subclasses are:
Framelayout.layoutparams, Linearlayout.layoutparams and Relativelayout.layoutparams.
We can write this:
ImageView ImageView = = new ImageView (GetContext ());
Linearlayout.layoutparams LP = new Linearlayout.layoutparams (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1); , 1 is optional.
Lp.setmargins (10, 20, 30, 40);
IMAGEVIEW.SETLAYOUTPARAMS (LP);
Go--android How to set margin in Java code