使用MarginLayoutParams设置View的margin

使用MarginLayoutParams设置View的margin

    ViewGroup.LayoutParams params = sdv_luck_activity.getLayoutParams();
    ViewGroup.MarginLayoutParams marginParams;
    if (params instanceof ViewGroup.MarginLayoutParams) {
        marginParams = (ViewGroup.MarginLayoutParams) params;
    } else {
        marginParams = new ViewGroup.MarginLayoutParams(params);
    }

    int oldMarginEnd = marginParams.getMarginEnd();
    int marginEnd = -DensityUtil.dipToPixels(mContext, 60);
    if (oldMarginEnd == marginEnd) {
        return;
    }
    marginParams.setMarginEnd(marginEnd);
    sdv_luck_activity.setLayoutParams(marginParams);

你可能感兴趣的:(Android基础篇)