美文网首页
Android-ConstraintLayout

Android-ConstraintLayout

作者: 森屿暖茶 | 来源:发表于2021-04-26 12:28 被阅读0次

ConstraintLayout(约束布局)

主要是为了解决布局嵌套过多的问题(布局优化、性能优化根本上的),以灵活的方式定位和调整小部件。从 Android Studio 2.3 起,官方的模板默认使用 ConstraintLayout。

意义

在开发过程中经常能遇到一些复杂的UI,可能会出现布局嵌套过多的问题,嵌套得越多,设备绘制视图所需的时间和计算功耗也就越多。简。因为我们UI的绘制机制是层层绘制的。而且由于viewGourp的嵌套,会导致计算功耗也增加。

常见属性(不做过多介绍,和其他布局的使用方式一样)

android_maxHeight

android_maxWidth

android_minHeight

android_minWidth

layout_marginStart

layout_marginEnd

layout_marginLeft

layout_marginTop

layout_marginRight

layout_marginBottom

常用属性

layout_constraintLeft_toLeftOf

layout_constraintLeft_toRightOf

layout_constraintRight_toLeftOf

layout_constraintRight_toRightOf

layout_constraintTop_toTopOf

layout_constraintTop_toBottomOf

layout_constraintBottom_toTopOf

layout_constraintBottom_toBottomOf

layout_constraintStart_toEndOf

layout_constraintStart_toStartOf

layout_constraintEnd_toStartOf

layout_constraintEnd_toEndOf

先介绍上述的常用属性,看着是不是跟相对布局非常的像?没错!你确实也差不多可以这么理解它,不过你需要注意的是,这些属性的意义是指:当前View(设置属性的View)的某个属性,在目标View(of)的某个属性那边。

举个栗子:

效果图

这样是不是很好理解。ConstraintLayout还有一些其他的属性。

layout_constraintBaseline_toBaselineOf

解法还是一样的,我的基准线在(of)的基准线上,也就是我和(of)的基准线一致。

效果图

GONE属性(被依赖控件GONE之后的边距属性)

layout_goneMarginBottom

layout_goneMarginEnd

layout_goneMarginLeft

layout_goneMarginRight

layout_goneMarginStart

layout_goneMarginTop

bias(偏移)

layout_constraintHorizontal_bias// 水平偏移

layout_constraintVertical_bias// 垂直偏移

在设置控件的居中属性之后,通过偏移属性可以设置让控件更偏向于依赖控件的某一方,偏移设置为0~1之间的值。有点类似线性布局的weight属性

尺寸属性:常规layout_width和layout_height的值:Xdp,wrap,match_parent,特殊的0dp时候。以下约束的宽高就生效了。

layout_constraintWidth_default

layout_constraintHeight_default

这2个属性只有在view 设置 android:layout_width="0dp" 和android:layout_height="0dp"时才会生效,它有3个值:

wrap(自适应):宽高不超过父容器。和传统的自适应差不多。

spread(伸展):意思是占用所有的符合约束的空间,在这个模式下使用百分比属性效果一样,百思不得其解。

percent (百分比):设置后以下这2个百分比属性生效,layout_constraintWidth_percent和layout_constraintHeight_percent,值为0~1之间,分别表示宽占父容器的百分比,高占父容器的百分比

角度定位(一般用的较少)

上面例子中的TextView2用到了3个属性:

app:layout_constraintCircle="@+id/TextView1"

app:layout_constraintCircleAngle="120"(角度)

app:layout_constraintCircleRadius="150dp"(距离)

指的是TextView2的中心在TextView1的中心的120度,距离为150dp,效果如下:

ConstraintLayout中有3个辅助控件协助我们布局

Guideline(布局辅助线)(也是基准线,基于parent)

可以理解为布局辅助线,用于布局辅助,不在设备上显示。

android:orientation=“vertical/horizontal”:有垂直和水平两个方向,当设置这个属性时

垂直:宽度为0,高度等于父容器

水平:高度为0,宽度等于父容器

有三种放置Guideline的方式:

layout_constraintGuide_begin: 控制 Guideline 距离父容器开始的距离

layout_constraintGuide_end: 控制 Guideline 距离父容器末尾的距离

layout_constraintGuide_percent: 控制 Guideline 在父容器中的位置为百分比

Barrier(屏障)(也是基准线,基于view)

app:barrierDirection:为屏障所在的位置,可设置的值有:bottom、end、left、right、start、top

app:constraint_referenced_ids:为屏障引用的控件,可设置多个(用“,”隔开)

Group(组)

可以把多个控件归为一组,方便隐藏或显示一组控件。

让代码更简洁,设置起来更方便。注意,组设置的显示或者隐藏会使组内控件的visible属性设置失效。

Placeholder(占位符)

使用代码动态改变内容。PS:如果我们使用代码动态改变内容,结合TransitionManager可以做一些有趣的过度动画等。

这样就可以使view慢慢跑过来

相关文章

网友评论

      本文标题:Android-ConstraintLayout

      本文链接:https://www.haomeiwen.com/subject/ikgcrltx.html