Android 如何在Java代码中手动设置控件的marginleft

1.定义LayoutParams

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);//定义一个LayoutParams

2.在LayoutParams中设置marginLeft

layoutParams.setMargins(20,0,0,0);//4个参数按顺序分别是左上右下

3、把这个LayoutParams设置给控件

mView.setLayoutParams(layoutParams); //mView是控件

你可能感兴趣的:(Android)