android 动态设置约束,ConstraintLayout 在代码中动态设置约束
ConstraintLayout的普及让Android的开发者们能更方便地进行布局,但如何在代码中设置ConstraintLayout的约束呢?网上的资料不太详细,在这里归纳总结一下。
ConstraintSet
这个类在官方文档上是这样描述的:
This class allows you to define programmatically a set of constraints to be used with ConstraintLayout.
For details about Constraint behaviour see ConstraintLayout. It lets you create and save constraints, and apply them to an existing ConstraintLayout. ConstraintsSet can be created in various ways:
Manually
c = new ConstraintSet(); c.connect(....);
from a R.layout.* object
c.clone(context, R.layout.layout1);
from a ConstraintLayout
c.clone(clayout);
此类允许你在代码中定义与ConstraintLayout一起使用的约束。而使用这个类的方法非常简单,你需要新建这个类,并操作这个类:
c = new ConstraintSet(); c.connect(....);
或者先从你的布局中把布局约束先克隆出来,再操作这个布局约束的克隆体:
c.clone(context, R.layout.layout1);
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
