1. 程式人生 > >Android在java程式碼中設定margin

Android在java程式碼中設定margin

然則有些景象下,須要在java程式碼裡來寫,可是View本身沒有setMargin辦法,怎麼辦呢?

經由過程查閱android api,我們發明android.view.ViewGroup.MarginLayoutParams有個辦法setMargins(left, top, right, bottom).

其直接的子類有: FrameLayout.LayoutParams, LinearLayout.LayoutParams and RelativeLayout.LayoutParams.

應用辦法:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 20, 30, 40);
imageView.setLayoutParams(lp); url:http://greatverve.cnblogs.com/archive/2012/01/29/android-margin.html