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);
c.clone(clayout);
这里有个地方一定要注意,你克隆出来的布局约束是“克隆的”,你之后对这个克隆约束的任何改变都是建立在之前的约束之上的,如果你要清空之前的约束,可以使用
c.
clear
(int viewId)
回到之前,把ConstraintSet克隆或者创建出来之后,我们该如何对这个对象进行设置操作呢?举个例子,如果我想让textView的右边界向整个父容器的右边界对齐,那么我可以这样写:
c.connect(textView.getId(),ConstraintSet.RIGHT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT);
如果我想在右边界添加间隔呢?可以这样写:
c.connect(textView.getId(),ConstraintSet.RIGHT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT,10);
这个时候我如果再想将它的上边界约束向button的下边界对齐呢?
c.connect(textView.getId(),ConstraintSet.TOP,button.getId(),ConstraintSet.BOTTOM);
我们再回头看看这个方法吧:
connect
(int startID, int startSide, int endID, int endSide, int margin)
|
startID就是你要约束的控件ID,startSide为他要约束的边界,而endID则是你的约束条件控件的ID,endSide为它的边界,margin自然就是间隔了。跟ConstraintLayout在xml中的约束不同,ConstraintSet的约束操作只用了这个方法——connect。
同时,ConstraintSet还能方便地设置你控件的宽高,使用
这两个方法即可。
当我们设置好控件的约束,如何将该约束应用呢?同样非常简单:
c.applyTo(mConstraintLayout);
只需要一行代码就能搞定。
ConstraintSet的设置方法还有许多,基本上能在XML里定义的都有,这里就不一一列举了,在最后给上官方文档地址,供各位查询ConstraintSet官方说明
ConstraintLayout的普及让Android的开发者们能更方便地进行布局,但如何在代码中设置ConstraintLayout的约束呢?网上的资料不太详细,在这里归纳总结一下。ConstraintSet 这个类在官方文档上是这样描述的:This class allows you to define programmatically a set of constra...
1. 首先要声明一下ConstraintSet对象private val set = ConstraintSet()
复制代码2. 其次需要复制一份父布局的约束,方法有三个如下:set.clone(constraintLayout: ConstraintLayout);
set.clone(set: ConstraintSet);
set.clone(context: Context, const...
一、什么是约束布局(ConstraintLayout)
ConstraintLayout 是一个使用“相对定位”灵活地确定微件的位置和大小的一个布局,在 2016 年 Google I/O 中面世,它的出现是为了解决开发中过于复杂的页面层级嵌套过多的问题——层级过深会增加绘制界面需要的时间,影响用户体验,以灵活的方式定位和调整小部件。
ConstraintLayout 是一个ViewGroup,可以在Api9以上的Android系统使用它,它的出现主要是为了解决布局嵌套过多的问题,以灵活的方式定位和调整小
Constraint Layout Samples
This repository contains a list of layouts that showcases the various features and usage of
ConstraintLayout
Pre-requisites
Android Studio 2.3
Constaint Layout library 1.0.1
Getting Started
Import the project using Android Studio. Navigate to the app>res>layout> and open one of the layouts
in the layout editor. This sample is best understood by seeing the constraints in the Design mode
of the layout editor.
Screenshots
Support
If you've found an error in this sample
约束布局ConstraintLayout 是一个ViewGroup,是Android 8 新增的布局方式,它的出现主要是为了解决布局嵌套过多的问题,以灵活的方式定位和调整小部件,是借鉴IOS所支持的约束的布局。从功能上讲,约束布局相当于相对布局的改进,谷歌官方也是推荐用约束布局来代替相对布局。
ConstraintLayout 官方文档
2.为什么要用ConstraintLayout
在开发过程中经常能遇到一些复杂的UI,可能会出现布局嵌套过多的问题,嵌套得越多,设备绘制视图所需的时间和计
3. 给TextView设置水平和垂直方向的约束条件,使其分别与容器的中心点对齐。
4. 设置TextView的宽度和高度,可以使用wrap_content或者具体的数值。
5. 最后,预览布局效果,可以看到TextView已经成功居中了。
示例代码如下:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="居中文本"
app:layout_constraintHorizontal_centerInParent="true"
app:layout_constraintVertical_centerInParent="true"/>
</androidx.constraintlayout.widget.ConstraintLayout>