android ConstraintLayout约束布局

记录一下ConstraintLayout的一些简单用法

摘自https://www.jianshu.com/p/b884b8c46584

https://cloud.tencent.com/developer/article/1150341

写的很详细 需要的可以重点看一下 写的很好

 

基础方法

  • layout_constraintBaseline_toBaselineOf (View A 内部文字与 View B 内部文字对齐)
  • layout_constraintLeft_toLeftOf (View A 与 View B 左对齐)
  • layout_constraintLeft_toRightOf (View A 的左边置于 View B 的右边)
  • layout_constraintRight_toLeftOf (View A 的右边置于 View B 的左边)
  • 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

约束的强度

  • layout_constraintHorizontal_bias
  • layout_constraintVertical_bias

Visibility 属性

  • Visibility 即使设置为gone也不会影响布局

宽高比

  • app:layout_constraintDimensionRatio
  • 要使用 layout_constraintDimensionRatio 属性,需要至少设置宽度或者高度为 0dp,
    宽高的尺寸比例可以通过“float值”或者“宽度:高度”的形式来设置

设置控件之间的宽高占比

  • app:layout_constraintHorizontal_weight="1.5"(类似于layout_weight)

 

锚向指示线

  • Guideline (其实就是加了一个指示线 集成view 不可见)

 

Circular Positioning(圆形定位)

  • app:layout_constraintCircle          -目标 View 的 ID
  • app:layout_constraintCircleAngle           -对齐的角度
  • app:layout_constraintCircleRadius         -与目标 View 之间的距离(顺时针方向,0~360度)

Enforcing constraints(强制约束)
在 1.1 版本之前,如果将控件的尺寸设置为 wrap_content,那么对控件设置的 maxWidth、minHeight 这些约束是不起作用的,
而强制约束就用于使控件在设置 wrap_content 的情况下约束依然生效

  • app:layout_constrainedHeight="true | false"
  • app:layout_constrainedWidth="true | false"

Dimensions
当控件的尺寸设置为 0dp 时,可以用以下两个属性来指定当前控件的宽度或高度占父控件的百分比,属性值在 0 到 1 之间

  • layout_constrainWidth_percent
  • layout_constrainHeight_percent

 

Barrier(屏障)
很多时候我们都会遇到控件的大小随着其包含的数据的多少而改变的情况,
而此时如果有多个控件之间是相互约束的话,就比较难来设定各个控件间的约束关系了
而 Barrier(屏障)就是用于这种情况,Barrier 和 GuideLine 一样是一个虚拟的 View,
对界面是不可见的,只是用于辅助布局,
而 Barrier 和 GuideLine 的区别在于它可以由多个 View 来决定其属性

  • barrierDirection:用于设置 Barrier 的位置,属性值有:bottom、top、start、end、left、right
  • constraint_referenced_ids:用于设置 Barrier 所引用的控件的 ID,可同时设置多个
  • barrierAllowsGoneWidgets:默认为 true,当 Barrier 所引用的控件为 Gone 时,则 Barrier 的创建行为是在已 Gone 的控件已解析的位置上进行创建。如果设置为 false,则不会将 Gone 的控件考虑在内

 

Group

Group 用于控制多个控件的可见性,如果要隐藏多个控件则不必再单独设置每个控件的可见性了,其使用到的属性有

  • constraint_referenced_ids :用于设置所引用的控件的 ID

 

Placeholder

Placeholder (占位符)用于和一个视图关联起来,通过 setContentId() 方法将占位符转换为指定的视图,即视图将在占位符所在位置上显示,如果此时布局中已包含该视图,则视图将从原有位置消失

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio的约束布局是一种强大而灵活的布局方式,它允许您以声明性的方式定义视图之间的关系。以下是使用约束布局的一些基本步骤: 1. 在布局文件中使用`ConstraintLayout`作为根视图。在XML文件中,您可以这样声明一个约束布局: ``` <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 在这里添加其他视图 --> </androidx.constraintlayout.widget.ConstraintLayout> ``` 2. 定义视图之间的约束关系。您可以通过拖动和放置视图来在设计编辑器中设置约束,也可以在XML文件中手动编写约束。例如,要将一个按钮位于父布局顶部,并与左右边缘有10dp的间距,您可以这样定义约束: ``` <Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="My Button" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_marginStart="10dp" app:layout_marginEnd="10dp"/> ``` 3. 可选地,您还可以使用链(chains)和辅助对象(guidelines)来进一步定义视图之间的关系,以及在不同屏幕尺寸下的自适应布局约束布局的优势在于它可以适应各种屏幕尺寸和方向,并且可以减少嵌套布局的需要。您可以通过在Android Studio的设计编辑器中直观地操作视图和约束,或者手动编辑XML文件来创建约束布局。要了解更多关于约束布局的信息和用法,请参阅Android官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值