然則有些景象下,須要在java代碼裏來寫,可是View本身沒有setMargin辦法,怎麼辦呢?
經由過程查閱android api,我們發明android.view.ViewGroup.MarginLayoutParams有個辦法setMargins(left, top, right, bottom).
其直接的子類有: FrameLayout.LayoutParams, LinearLayout.LayoutParams and RelativeLayout.LayoutParams.
應用辦法:
lp.setMargins(10, 20, 30, 40);
imageView.setLayoutParams(lp);
Admin