We can usually set the margin directly in XML, such as:
<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:
New linearlayout.layoutparams (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); Lp.setmargins (ten); IMAGEVIEW.SETLAYOUTPARAMS (LP);
If this method has no effect, you can use the control to modify the margin, such as the previous ImageView, there is a GetLayout method, the layout obtained in the strong to linearlayout or other, and then set the margin.
I'm the dividing line of the king of the Land Tiger.
Reproduced
Android--Java code settings margin