1LinearLayout ll = new LinearLayout(this);
2ll.setOrientation(LinearLayout.VERTICAL);
3
4LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
5 LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
6
7layoutParams.setMargins(30, 20, 30, 0);
8
9Button okButton=new Button(this);
10okButton.setText("some text");
11ll.addView(okButton, layoutParams);
12
1int sizeInDP = 16;
2
3int marginInDp = (int) TypedValue.applyDimension(
4 TypedValue.COMPLEX_UNIT_DIP, sizeInDP, getResources()
5 .getDisplayMetrics());
6setMargins(view, sizeInDP, sizeInDP, sizeInDP, sizeInDP);
7
8
9void setMargins (View view, int left, int top, int right, int bottom) {
10 if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
11 ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
12 p.setMargins(left, top, right, bottom);
13 view.requestLayout();
14 }
15}
16
1ViewGroup.LayoutParams p = this.getLayoutParams();
2 if (p instanceof LinearLayout.LayoutParams) {
3 LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)p;
4 if (_default) lp.setMargins(mc.oml, mc.omt, mc.omr, mc.omb);
5 else lp.setMargins(mc.ml, mc.mt, mc.mr, mc.mb);
6 this.setLayoutParams(lp);
7 }
8 else if (p instanceof RelativeLayout.LayoutParams) {
9 RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)p;
10 if (_default) lp.setMargins(mc.oml, mc.omt, mc.omr, mc.omb);
11 else lp.setMargins(mc.ml, mc.mt, mc.mr, mc.mb);
12 this.setLayoutParams(lp);
13 }
14 else if (p instanceof TableRow.LayoutParams) {
15 TableRow.LayoutParams lp = (TableRow.LayoutParams)p;
16 if (_default) lp.setMargins(mc.oml, mc.omt, mc.omr, mc.omb);
17 else lp.setMargins(mc.ml, mc.mt, mc.mr, mc.mb);
18 this.setLayoutParams(lp);
19 }
20}
21