Android 对ConstraintLayout 下的视图 topMargin 无效但是在layoutxml 中可以设定回到java代码中就不行了
这可能是因为在代码中设置的 topMargin 值被后续的 ConstraintLayout 约束覆盖了。可以尝试在代码中设置 ConstraintLayout 的约束条件来解决这个问题,例如:
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) view.getLayoutParams();
layoutParams.topMargin = 100;
layoutParams.topToBottom = R.id.some_other_view;
view.setLayoutParams(layoutParams);
其中,topToBottom 指定了当前视图的顶部应该连接到某个其他视图的底部,这样就可以保证 topMargin 生效了。
原文地址: https://www.cveoy.top/t/topic/dobt 著作权归作者所有。请勿转载和采集!