轻松玩转ConstraintLayout(一)基础篇

在这里插入图片描述
ConstraintLayout自从2016年Google/IO推出以来,已逐渐成为Android开发的首选布局。它与RelativeLayout相似但是性能方面更优,而且AndroidStudio提供LayoutEditor可以达到像iOS上AutoLayout一样的开发体验。

Our performance comparison shows that ConstraintLayout performs about 40% better in the measure/layout phase than RelativeLayout:

虽然优点诸多,但是其复杂多样的属性导致使用上有一定门槛。所有我希望通过两篇文章将ConstraintLayout的基本使用方式讲明白,让大家在日常开发中不会再谈虎色变。

轻松玩转ConstraintLayout(一)基础篇
轻松玩转ConstraintLayout(二)示例篇

本篇作为基础篇,先简单介绍一下ConstraintLayout中的基本属性

  • 位置属性
  • 间隔属性
  • 比例属性
  • 排列属性

位置属性


在这里插入图片描述
位置属性主要用来描述ConstraintLayout中各个View之间的相对位置

layout_to

用来指定在某目标View的左侧或右侧,也可以目标View也可以是父容器。例如
layout_constraintStart_toEndOf="parent"layout_constraintStart_toEndOf="@id/hoge"

相当于RelativeLayout的layout_to

ConstraintLayout功能RelativeLayout
layout_constraintEnd_toStartOf位于目标的左侧layout_toStartOf
layout_constraintStart_toEndOf位于目标的右侧layout_toEndOf
layout_constraintRight_toLeftOf位于目标的左侧layout_toLeftOf
layout_constraintLeft_toRightOf位于目标的右侧layout_toRightOf
layout_constraintBottom_toTopOf位于目标的上侧layout_above
layout_constraintTop_toBottomOf位于目标的下侧layout_below

layout_align

将上述的属相稍作变化就可以表示与目标View的左侧或右侧对齐,相当于RelativeLayout的layout_align

ConstraintLayout功能RelativeLayout
layout_constraintStart_toStartOf与目标左对齐layout_alignStart
layout_constraintEnd_toEndOf与目标右对齐layout_alignEnd
layout_constraintLeft_toLeftOf与目标左对齐layout_alignLeft
layout_constraintRight_toRightOf与目标右对齐layout_alignRight
layout_constraintTop_toTopOf与目标上对齐layout_alignTop
layout_constraintBottom_toBottomOf与目标下对齐layout_alignBottom

居中

在这里插入图片描述
如果相对于父容器同时左对齐和右对齐,则可以实现左右居中效果,上下居中也同理

<TextView android:id="@+id/text_view"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

Baseline

基线对齐,类似RelativeLayout的Baseline

ConstraintLayout功能RelativeLayout
layout_constraintBaseline_toBaselineOf与目标的对齐基线layout_alignBaseline

间隔属性


子View之间的间隔主要分为两类:MarginGoneMargin

Margin

在这里插入图片描述
就像常规布局的margin属性一样

属性值类型
android:layout_marginLeftdp
android:layout_marginRightdp
android:layout_marginStartdp
android:layout_marginEnddp
android:layout_marginTopdp
android:layout_marginBottomdp

GoneMargin

GoneMargin表示会跟随目标对象Visibility:Gone而消失,不会给剩余可见部分的布局带来影响
在这里插入图片描述

属性取值类型
android:layout_goneMarginLeftdp
android:layout_goneMarginRightdp
android:layout_goneMarginStartdp
android:layout_goneMarginEnddp
android:layout_goneMarginTopdp
android:layout_goneMarginBottomdp

比例属性


比例主要包括两类

  • bias :位置占比(也可以视为位置属性)
  • ration:size占比

bias

前面我们知道通过位置属性,可以设置子View居中。此时通过bias,可以设置居中偏移量,通过偏移量可以实现一个相对于父容器位置占比的效果

属性功能
layout_constraintHorizontal_bias水平方向bias
layout_constraintVertical_bias竖直方向bias

bias接受0~1之间的值,例如左侧偏移30%
在这里插入图片描述

<TextView android:id="@+id/text_view"
    app:layout_constraintHorizontal_bias="0.3"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

ration

ConstraintLayout中有3中方式来设置子子View的宽高尺寸:

  • Xdp,X为具体数值
  • WARP_CONTENT
  • 0dp,0dp代表MATCH_CONSTRAINT,ConstraintLayout不推荐使用MATCH_PARENT

用0dp代表MATCH_CONSTRAINT,意味着这个实际size需要通过其他方式决定,此时可以用ration来指定。

属性取值类型
layout_constraintDimensionRatioX:Y
<Button 
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="1:1" />

ration的值默认代表宽高比,即w:h。但是当width和height都为MATCH_CONSTRAINT时,需要指定是w:h还是h:w

例如如下布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        android:text="TextView"
        app:layout_constraintDimensionRatio="h,3:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="0dp" />

</android.support.constraint.ConstraintLayout>

在这里插入图片描述

app:layout_constraintDimensionRatio="h,3:1"h,代表以w为基础去设置h,即h = w * ratio。 w,则意义相反


排列属性


所谓排列属性是指如何对多个子View之间进行约束达到某种布局的排列效果,这主要是依靠chainStyle来完成的

chainStyle

多个子View之间的约束称为chainchainStyle子View之间的约束呈现了何种排列类型。

在这里插入图片描述

属性功能
layout_constraintHorizontal_chainStyle水平方向类型
layout_constraintVertical_chainStyle竖直方向类型

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintHorizontal_chainStyle="packed" />

chainStyle有多种类型,通过下图可以一目了然
在这里插入图片描述

类型功能
spread平分剩余空间
spread_inside两端对齐,内部均分
packed内部紧贴,外部均分

weighted chain

当chainStyle指定为spread或者spread_inside时,可以用layout_constraintHorizontal_weight或者layout_constraintHorizontal_height来指定子View相对于剩余空间的占比(对剩余空间的瓜分)

Packed Chain with Bias

当chainStyle指定为packed时,可以用layout_constraintHorizontal_bias或者layout_constraintVertical_bias来指定居中偏移量


最后


ConstraintLayout的基础篇就介绍完了,接下来的示例篇会介绍如何用ConstraintLayout实现一些常用的布局

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fundroid

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值