首页 > 解决方案 > 如何以编程方式设置 ConstraintLayout 的 XML 属性“layout_constrainedWidth”?

问题描述

标签: androidandroid-constraintlayout

解决方案


如果您想以constrainedWidth/Height编程方式设置,那么您必须根据自己的意愿ConstraintLayout.LayoutParams设置名为constrainedWidthconstrainedHeight的标志。

IE

ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) myView.getLayoutParams(); // View for which we need to set constrainedWidth.
lp.constrainedWidth = true/false;
myView.setLayoutParams(lp);

约束宽度

如果设置了左右约束并且小部件尺寸不是固定尺寸,则指定水平尺寸是否受到约束。

默认情况下,如果一个小部件设置为WRAP_CONTENT,我们会将该维度视为一个固定维度,这意味着无论约束如何,维度都不会改变。

将此属性设置为true 允许更改维度以遵守约束。

在这里查看。


推荐阅读