Android ConstraintLayout

0dp 充满约束

to左侧是控件本身,to右边是约束条件

例如:

复制代码
app:layout_constraintBottom_toBottomOf="@id/image_1"
app:layout_constraintTop_toBottomOf="@id/image_1"

指定权重:

复制代码
app:layout_constraintHorizontal_weight=""
复制代码
app:layout_constraintVertical_weight=""

baseLine :基准线

复制代码
app:layout_constraintBaseline_toBaselineOf="@id/image_1" 基准线对齐

以圆形为基准进行约束

复制代码
app:layout_constraintCircle="@id/image_1" //控件
app:layout_constraintCircleAngle="200" //角度
app:layout_constraintCircleRadius="40dp" //距离

宽高约束:

复制代码
app:layout_constrainedWidth="true" 宽度不超过相对的控件
复制代码
app:layout_constrainedHeight="true"宽度不超过相对的控件

位移

复制代码
app:layout_constraintVertical_bias="" 纵向位移0.0 -1.0
复制代码
app:layout_constraintHorizontal_bias="" 横向位移0.0 -1.0

当约束控件隐藏GONE时 设置间距 非GONE时不生效

复制代码
app:layout_goneMarginStart="10dp"

约束链条空间

复制代码
layout_constraintHorizontal_chainStyle 默认 spread

可以配合bias使用 设置方向位移

也可以使用weight 设置权重

layout_constraintHorizontal_weight

layout_constraintVertical_weight

宽高比

复制代码
layout_constraintDimensionRatio="16:9" 设置宽高比 比如 16:9 但是宽和高至少有一个为0dp,也可以配合"W:1:1" 表示宽计算出来,分配高度,或者"H,2:1" 表示高度计算出需要分配宽度

下面这种也要设为宽or 高 为 0dp

然后设置宽高比

复制代码
app:layout_constraintWidth_percent="0.5"
复制代码
app:layout_constraintHeight_percent="0.5"

Guideline 基准线

复制代码
<androidx.constraintlayout.widget.Guideline
    android:orientation="vertical"
复制代码
    app:layout_constraintGuide_end="120dp" 结束位置
    app:layout_constraintGuide_begin="120dp" top left 间距
    app:layout_constraintGuide_percent="0.2" 间距20%
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

ConstraintHelper 约束空间辅助类

复制代码
<androidx.constraintlayout.widget.Group
    app:constraint_referenced_ids="image_1,id2"/>

Group如果GONE或者VISIABLE ,则设置的ids也会对应显示或隐藏

然后可以再代码里设置动画等,比如 translationX,Y rotation等

Barrier 也是属于 ConstraintHelper

复制代码
<androidx.constraintlayout.widget.Barrier
    app:barrierDirection="end"
    app:constraint_referenced_ids="image_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

可以设置ids,然后通过barrierDirection设置ids View的约束位置 比如 end ,start 等

动态设置动画:ConstraintHelper

然后设置一组View的ID

Placeholder 占位控件 动态替换控件

复制代码
<androidx.constraintlayout.widget.Placeholder
    android:id="@+id/placeholder"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

设置ID

ConstraintSet:

通过创建Set 然后设置参数,最后apply保存

Flow:VirtualLayout : COnstraintHelper

复制代码
<androidx.constraintlayout.helper.widget.Flow
    app:flow_wrapMode="aligned" //约束条件 设置展示方式
    app:flow_verticalGap="16dp" 垂直间距
    app:flow_horizontalGap="16dp" 水平间距
    android:orientation="vertical" 方向
    app:constraint_referenced_ids="image_1" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

通过设置ids

Clone:通过ConstraintSet 进行clone 别的布局的约束条件 然后替换为自身的

idForceId 如果克隆的View没有ID 则设置这个参数 否则报错

过渡动画:

TransitionManager.beginDelayedTransition(布局) 设置动画效果

相关推荐
千里马学框架13 小时前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台13 小时前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone14 小时前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc14 小时前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo16 小时前
Kotlin 协程中的 Job 结构化并发与取消
android
sun00770016 小时前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼17 小时前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone18 小时前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen18 小时前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone18 小时前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui