我通过扩展ViewGroup创建了一个自定义viewGroup,但即使我可以访问LayoutParams,我似乎也无法使用该对象的setMargins属性。
答案 0 :(得分:1)
如果您可以访问LayoutParams
(它们不是null
),请尝试将它们转换为MarginLayoutParams
。大多数LayoutParams
(例如LinearLayout.LayoutParams
,RelativeLayout.LayoutParams
延长MarginLayoutParams
)
MarginLayoutParams params = (MarginLayoutParams) getLayoutParams();
params.setMargins(left, top, right, bottom);
requestLayout();
答案 1 :(得分:0)
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
yourView.setLayoutParams(params);
希望它会有所帮助!