如何以编程方式设置ConstraintLayout的XML属性'layout_constrainedWidth'? [英] How to set the XML attribute 'layout_constrainedWidth' of ConstraintLayout programmatically?

查看:232
本文介绍了如何以编程方式设置ConstraintLayout的XML属性'layout_constrainedWidth'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在约束布局"中,如何转换xml属性:

In Constraint Layout, how to convert the xml attribute:

app:layout_constrainedWidth=true|false

用代码?

推荐答案

我刚刚发现了这个问题.

I just figured this out.

因此,根据以上答案,

constrainedWidth可以通过ConstraintLayout.LayoutParams访问,因此可以通过以下代码进行更改:(谢谢@JeelVankhede)

constrainedWidth can be accessed through ConstraintLayout.LayoutParams and thus can be altered by the following code: (Thank you @JeelVankhede)

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

但是,使用ConstraintsSet时,需要以下代码:

However when using ConstraintsSet the following code would be needed:

val constraintLayout = // your constraint layout
val constraintSet = ConstraintSet()
constraintSet.clone(constraintLayout)

// This is the imporatant part
constraint.constrainDefaultWidth(R.id.yourViewId, ConstraintSet.MATCH_CONSTRAINT_WRAP)

constraintSet.applyTo(constraintLayout)

这篇关于如何以编程方式设置ConstraintLayout的XML属性'layout_constrainedWidth'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆