这也是android适配的一种方法 :
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(btn_back.getLayoutParams());
lp.setMargins(0, 10, 0, 0);
btn_back.setLayoutParams(lp);
}
简洁的工具方法:
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();
}
}