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是控件
