RelativeLayout.LayoutParams LinearLayout.LayoutParams

如果父布局是LinearLayout

                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) getLayoutParams();
                params.leftMargin = 50;
                params.rightMargin = 100;
                setLayoutParams(params);

如果父布局是RelativeLayout, 直接设置leftMargin,rightMargin没有效果

                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) getLayoutParams();
                layoutParams.setMargins(getLeft() + offsetX, getTop() + offsetY,0,0);
                setLayoutParams(layoutParams);

你可能感兴趣的:(RelativeLayout.LayoutParams LinearLayout.LayoutParams)