Java CONSTRAINTLAOUT设置constraintHeight_默认值以编程方式
我有一个ConstraintLayout,它包含一个Java CONSTRAINTLAOUT设置constraintHeight_默认值以编程方式,java,android,android-layout,android-constraintlayout,Java,Android,Android Layout,Android Constraintlayout,我有一个ConstraintLayout,它包含一个ViewStub,在XML中将app:constraintHeight\u default属性设置为spread。我需要能够在运行时更新此属性,使其值为wrap,但到目前为止,没有任何东西对我有效。布局如下: 到目前为止,我已经尝试使用ConstraintSet以编程方式设置constraintHeight\u默认值,但是从ViewStub膨胀的布局最终具有0宽度和0高度,因此它不会在布局中渲染 ConstraintLayout Cons
ViewStub
,在XML中将app:constraintHeight\u default
属性设置为spread
。我需要能够在运行时更新此属性,使其值为wrap
,但到目前为止,没有任何东西对我有效。布局如下:
到目前为止,我已经尝试使用ConstraintSet
以编程方式设置constraintHeight\u默认值
,但是从ViewStub
膨胀的布局最终具有0宽度和0高度,因此它不会在布局中渲染
ConstraintLayout ConstraintLayout=root.findviewbyd(R.id.constraint\u布局);
ConstraintSet ConstraintSet=新ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.CONSTRAINTHEIGH(R.id.view\U存根、constraintSet.MATCH\U CONSTRAINT\U WRAP);
constraintSet.applyTo(constraintLayout);
ViewStub ViewStub=root.findViewById(R.id.view\u stub);
viewStub.setLayoutResource(R.layout.custom_布局);
视图存根。充气();
如何设置
视图存根的高度以编程方式包装其高度?尝试更改以下行:
constraintSet.constrainHeight(R.id.view_stub, ConstraintSet.MATCH_CONSTRAINT_WRAP);
限制高度
constraintSet.constrainDefaultHeight(R.id.view_stub, ConstraintSet.MATCH_CONSTRAINT_WRAP);
看
约束默认高度
constraintSet.constrainDefaultHeight(R.id.view_stub, ConstraintSet.MATCH_CONSTRAINT_WRAP);
无效约束默认高度(int viewId,
整数高度)
设置高度的计算方式为“匹配约束环绕”或“匹配约束展开”。违约是分散的
好奇的如果你充气然后更新约束高度会发生什么?@stkent同样的情况也会发生。就是这样!感谢这解决了我试图从wrap\u content
更改为match\u constraint
(0dp)的问题。