ConstraintLayout记录

一、概述

ConstraintLayout字面意思就是约束布局,约束布局可以说是RelativeLayout的进阶版,但性能比RelativeLayout更强大。在以往的布局里,主要是通过程序员编写XML的代码来实现,而约束布局则更偏向于用可视化的方式来编程。通过可视化的方式,更增加了布局的灵活性。

二、与其他布局的一些区别

  1. ConstraintLayout更偏向于可视化编程
    ConstraintLayout可以通过在Design页面拖拽控件,然后,约束布局可以通过控件与控件之间,控件与其它参考线之间进行约束,这样更加容易实现界面的设置
  2. 可以减少布局的嵌套
    约束布局只需要通过拖拽就可以实现所有控件的布置,可以减少在编程中各种布局的嵌套,加载的速度更加快。
  3. 界面的设置更加灵活
    每一个控件都可以有多个方向设置约束,而且也可以用参考线,偏移度等来实现每一个控件的布置,这样可以使界面的设置更加灵活。

三、ConstraintLayout的基本内容

1. 相对定位

相对定位是在ConstraintLayout中创建布局的最基本构建块,也就是一个控件相对于另一个控件进行定位,可以从横向、纵向添加约束关系,这也是和RelativeLayout相似的地方,可以通过Left、Right、Start、End、Top、Bottom、Baseline这七个属性,来实现各控件之间的相对定位。

属性
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_constraintBaseline_toBaselineOf
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf

这些属性都要结合id值或parent才可以实现,如:

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
app:layout_constraintRight_toLeftOf="button2"/>

这几行代码可以实现button1的右端向button2的左端约束
而:

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
app:layout_constraintRight_toRightOf="button2"/>

这两段只是最后一行有差别,则表示button1和button2右对齐

2. Margins

这里可以表示成两个约束控件之间距离的最小值或者说是间隔,单位是dp

属性
android:layout_marginStart
android:layout_marginEnd
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom

3. 偏向值

这也就是bias,当一对方向(左右,顶部底部其中一对)都添加约束时,就可以控制两边的比例值为0%~100%,表示约束的部件更偏向于哪一边越靠近0表示越靠近顶部或左边,100则反之

属性
app:layout_constraintVertical_bias=""
app:layout_constraintHorizontal_bias=""

这里的“”里可以表示0~1的值,0即为0%,1即为100%

4. 尺寸约束

这里可以分为三个部分,第一个是设置最小的尺寸,即minWidth和minHeight(这个只有在wrap_content时有效);第二个是通过对约束条件的设置,这个其实在上面已经有提及,就是通过对两边的约束,达到大小对齐的状态
如:

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"/>
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="button2"
app:layout_constraintRight_toRightOf="@+id/button1"
app:layout_constraintLeft_toLeftOf="@+id/button1"/>

这里可以实现button2的大小对齐button1,也可以设置间隔来使大小不同
第三个是设置宽高比例,顾名思义就是空间的宽和高的大小之比
属性

app:layout_constraintDimensionRatio="3:1"

即宽比高等于3比1,这里的layout_width和layout_height要有一个为0
如果两个相对的方向都有约束则要加上H和W来表示基准值

5. Guideline

这个相当于一个辅助线,分为水平和垂直两种,它可以帮助对于某些特定位置的约束,也就是说增加一条边,多一个约束的地方,使控件的约束更灵活

属性
layout_constraintGuide_begin
layout_constraintGuide_end
layout_constraintGuide_percent

上面前两个表示距离顶部或左边的距离和距离底部和右边的距离,以dp为单位,第三个这是表示宽高的百分比,以比例为单位可以填0~1

<android.support.constraint.Guideline
 android:id="@+id/guideline"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="vertical" 
 app:layout_constraintGuide_percent="0.5"/>

这个代码表示增加一条垂直在中间的辅助线,控件也可以以这条边来增加约束

6.链条Chain

这一部分我看得不怎么熟,我自己理解就是建立一个双向的链接,把两个控件连成一个整体,如果水平建立了链接,那么就是能在垂直方向上移动,如果是垂直方向上建立链接,那就只能在水平方向上移动控件,如果两个方向都有链接,就不能直接移动,要通过修改其他数值才可以,例如Margins值,其中,最左边(或顶部)的控件是链条的头。链条的种类分为Spread Chain,Spread Inside Chain,Weighted Chain,Packed Chain,Packed Chain with Bias这几种

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值