Android代码修改ConstraintLayout约束关系

Android代码修改ConstraintLayout约束关系,第1张

Android代码修改ConstraintLayout约束关系
//代码修改ConstraintLayout约束关系
ConstraintSet set = new ConstraintSet();
//克隆父布局(ConstraintLayout)的约束关系
set.clone(parentView);
//清除childView布局文件里设置的某个约束
set.clear(childView, ConstraintSet.TOP);
//设置新的约束关系,下面效果相当于 app:layout_constraintBottom_toBottomOf="parent"
set.connect(childView, ConstraintSet.BOTTOM,
        ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
//应用到父布局
set.applyTo(parentView);

注意某些情况下需要用set.clear清除布局文件设置的约束,否则会冲突得不到正确的效果

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5482672.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存