1.setPading
Button btn1 = (Button) findViewById(R.id.magic);
btn1.setPadding(10, 10, 10, 10);
2.setMargin
Button btn1 = (Button) findViewById(R.id.magic);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(btn1.getLayoutParams());
lp.setMargins(10, 10, 10, 10);
lp.leftMargin=10;
lp.rightMargin=10;
lp.topMargin=10;
lp.bottomMargin=10;
这个
LayoutParams是根据该view在不同的GroupView而不同的
可以把设置margin的方式封装成方法,只要是GroupView里面的LayoutParams 即可。
public static void setMargins (View v, int l, int t, int r, int b) {
if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
p.setMargins(l, t, r, b);
v.requestLayout();
}
}
Button btn1 = (Button) findViewById(R.id.magic);
btn1.getLayoutParams().width=100;
btn1.getLayoutParams().height=100;
QQ交流群:6399844