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(布局) 设置动画效果

相关推荐
m0_7482404433 分钟前
小皮面板Noinputfilespecified错误终极解决
android
xunyan623437 分钟前
安卓冷启动流程(Android 15)
android
郭优秀的笔记1 小时前
html鼠标悬浮提示功能
android·javascript·html
冬奇Lab1 小时前
Android 15存储子系统深度解析(一):Vold与存储管理框架
android·车载系统·系统架构
得一录1 小时前
Android 方向控制控件
android
美狐美颜SDK开放平台2 小时前
跨平台开发实战:直播美颜sdk动态贴纸在 Android / iOS / HarmonyOS 的落地方案
android·ios·harmonyos·美颜sdk·直播美颜sdk·视频美颜sdk·美颜api
2501_916008892 小时前
在不越狱前提下导出 iOS 应用文件的过程,访问应用沙盒目录,获取真实数据
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_915106322 小时前
Android和IOS 移动应用App图标生成与使用 Assets.car生成
android·ios·小程序·https·uni-app·iphone·webview
灵感菇_2 小时前
深入解析 Android事件分发机制
android·ui
TheNextByte12 小时前
如何在Mac上获取Android消息
android·macos