Android最强布局——ConstraintLayout约束布局

ConstraintLayout

首先,现附上官方文档:ConstraintLayout官方文档

约束布局ConstraintLayout 是一个ViewGroup,可以在Api9以上的Android系统使用它,它的出现主要是为了解决布局嵌套过多的问题,以灵活的方式定位和调整小部件。从 Android Studio 2.3 起,官方的模板默认使用 ConstraintLayout。

然而,ConstraintLayout的出现就是为了解决布局嵌套的问题。在开发过程中经常能遇到一些复杂的UI,可能会出现布局嵌套过多的问题,嵌套得越多,设备绘制视图所需的时间和计算功耗也就越多。

在使用过程中,ConstraintLayout 可以看做是一个更强大的 RelativeLayout,它提供了更多的 API 来约束控件的相对关系,更容易满足复杂的页面布局。

相对定位:

a控件的xx位于b控件的xx

layout_constraintLeft_toLeftOf a控件的左边位于b控件的右边
layout_constraintLeft_toRightOf
layout_constraintRight_toLeftOf
layout_constraintRight_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf
文本对齐:两个TextView的高度不一致,但是又希望他们文本对齐
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf

居中:

控件上下左右居中显示:
app:layout_constraintLeft_toLeftOf="parent”
app:layout_constraintRight_toRightOf=“parent”
app:layout_constraintTop_toTopOf=“parent”
app:layout_constraintBottom_toBottomOf=“parent”

居中偏移(bias)

在上述居中的情况下,可以设置偏移量
(0-1)0表示最左,1表示最右
layout_constraintHorizontal_bias 水平偏移
layout_constraintVertical_bias 垂直偏移

圆形定位(角度定位)

可以让一个控件以另一个控件的中心为中心点,来设置其相对与该中心点的距离和角度

  1. app:layout_constraintCircle 需要看齐的参照物,图中 B 就是把 A 当做参照物进行约束的
  2. app:layout_constraintCircleAngle 要旋转的角度,最上方 0 度,默认就是 0 度,顺时针开始算。
  3. app:layout_constraintCircleRadius 两个控件中心点的距离

边距:

1.控件必须在布局里约束一个相对位置
2.margin只能大于等于0
android:layout_marginStart
android:layout_marginEnd
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom

goneMargin

goneMargin主要用于约束的控件可见性被设置为gone的时候使用的margin值,属性如下:
layout_goneMarginStart
layout_goneMarginEnd
layout_goneMarginLeft
layout_goneMarginTop
layout_goneMarginRight
layout_goneMarginBottom

尺寸约束:

1.使用指定的尺寸
2.使用wrap_content,让控件自己计算大小
当控件的高度或宽度为wrap_content时,可以使用下列属性来控制最大、最小的高度或宽度:
android:minWidth 最小的宽度
android:minHeight 最小的高度
android:maxWidth 最大的宽度
android:maxHeight 最大的高度
注意!当ConstraintLayout为1.1版本以下时,使用这些属性需要加上强制约束,如下所示:
app:constrainedWidth=”true”
app:constrainedHeight=”true”
3.使用 0dp (MATCH_CONSTRAINT)
官方不推荐在ConstraintLayout中使用match_parent,可以设置 0dp

宽高比

当宽或高至少有一个尺寸被设置为0dp时,app:layout_constraintDimensionRatio=“1:1”
设置控件宽高比

约束链

能够在水平或垂直方向控件之间相互约束而组成的一条链就是约束链,约束链是由开头的控件进行属性控制的。没错就是跟着大哥走
app:layout_constraintHorizontal_chainStyle=“xxx”

  • packed:控件紧挨在一起。还可以通过bias属性设置偏移量。
  • spread:均与分布控件。
  • spread_inside:均与分布控件,但是两边控件贴边。

app:layout_constraintHorizontal_weight=“x”
app:layout_constraintVertical_weight=“x”
当宽度设为0dp,就可以使用水平权重。
当长度设为0dp,就可以使用垂直权重

辅助工具

Group

Group可以把多个控件归为一组,方便隐藏或显示一组控件
<android.support.constraint.Group
android:id="@+id/group"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:visibility=“invisible” 隐藏但存在在 gone 隐藏不存在
app:constraint_referenced_ids=“TextView1,TextView3” />

Guideline

Guideline 是约束布局中一个特殊的辅助布局类,可以创建水平或者垂直的参考线,其他的控件可以根据这个参考线来进行布局,它本质是不可见的控件。
参考线的位置属性:

orientation:vertical/horizontal
layout_constraintGuide_begin 指定距离左/上边开始的固定位置
layout_constraintGuide_end 指定距离右/下边开始的固定位置
layout_constraintGuide_percent 指定位于布局中所在的百分比

Barrier

constraint_referenced_ids 引用多个控件,看作一个整体来添加一个与另外一个控件限制最大宽/高的约束。

通过 app:barrierDirection 属性来决定 Barrier 的方向

设有3个控件ABC,C在AB的右边,但是AB的宽是不固定的,这个时候C无论约束在A的右边或者B的右边都不对。当出现这种情况可以用Barrier来解决。Barrier可以在多个控件的一侧建立一个屏障
在这里插入图片描述

  <android.support.constraint.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="right"
        app:constraint_referenced_ids="TextView1,TextView2" />

app:barrierDirection为屏障所在的位置,可设置的值有:bottom、end、left、right、start、top
app:constraint_referenced_ids为屏障引用的控件,可设置多个(用“,”隔开)

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android的Widget是指可以被放置在桌面或者其他应用中的小部件,比如常见的时钟、天气、日历等等。在Android中,Widget的布局可以使用XML文件来进行定义,与普通的布局定义类似,可以使用各种属性来设置Widget的样式和行为。 在Widget的XML布局文件中,可以使用View类中定义的许多属性来控制Widget的样式和行为,比如: 1. android:id:设置Widget的ID,可以在代码中通过findViewById()方法来获取对应的View对象。 2. android:layout_width、android:layout_height:设置Widget的宽度和高度,可以使用具体数值或者match_parent、wrap_content等特殊值。 3. android:layout_gravity:设置Widget在父布局中的对齐方式,比如center、left、right等等。 4. android:padding、android:paddingLeft、android:paddingRight等:设置Widget的内边距,用于控制Widget内部内容的显示位置。 5. android:background:设置Widget的背景颜色或者背景图片。 6. android:clickable、android:longClickable:设置Widget是否可以被点击或者长按。 7. android:focusable、android:focusableInTouchMode:设置Widget是否可以获取焦点,用于控制Widget是否可以响应键盘事件等。 8. android:visibility:设置Widget是否可见,可以使用值为visible、invisible、gone。 除了以上列出的属性之外,还有许多其他的属性可以用于控制Widget的样式和行为,具体可以查看Android官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值