android.view.ViewGroup.MarginLayoutParams
has a method setMargins(left, top, right, bottom)
. Direct subclasses are: FrameLayout.LayoutParams
, LinearLayout.LayoutParams
and RelativeLayout.LayoutParams
.
Using e.g. LinearLayout
:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
imageView.setLayoutParams(lp);
MarginLayoutParams
This sets the margins in pixels. To scale it use
context.getResources().getDisplayMetrics().density
DisplayMetrics
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…