美文网首页
[Android]带你了解 Android 约束布局 Const

[Android]带你了解 Android 约束布局 Const

作者: 黑森林中的小木屋 | 来源:发表于2018-09-19 10:06 被阅读0次

    注:本文是玉刚说的副本

    ConstraintLayout是Android新推出的一个布局,其性能更好,连官方的hello world都用ConstraintLayout来写了。所以极力推荐使用ConstraintLayout来编写布局。

    本文主要介绍一下如何使用代码来编写ConstraintLayout布局。

    好了,开始我们的征程。

    ConstraintLayout简介

    ConstraintLayout,可以翻译为约束布局,在2016年Google I/O 大会上发布。我们知道,当布局嵌套过多时会出现一些性能问题。之前我们可以去通过RelativeLayout或者GridLayout来减少这种布局嵌套的问题。现在,我们可以改用ConstraintLayout来减少布局的层级结构。ConstraintLayout相比RelativeLayout,其性能更好,也更容易使用,结合Android Studio的布局编辑器可以实现拖拽控件来编写布局等等。

    如果我们是新建工程,则Android Studio会默认帮我们加入ConstraintLayout的依赖了。如果我们是改造旧项目,可以在build.gradle中添加以下依赖:

        implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    

    然后就可以使用ConstraintLayout了。

    相对位置

    要在ConstraintLayout中确定view的位置,必须至少添加一个水平和垂直的约束。每一个约束表示到另一个view,父布局,或者不可见的参考线的连接或者对齐。如果水平或者垂直方向上没有约束,那么其位置就是0。

    我们先来看个例子:

    <?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"    
    android:layout_width="match_parent"    
    android:layout_height="match_parent">  
      <Button        
    android:layout_width="wrap_content"        
    android:layout_height="wrap_content"        
    android:text="左对齐"        
    app:layout_constraintLeft_toLeftOf="parent"        
    app:layout_constraintTop_toTopOf="parent"/>    
    
    <Button        
    android:layout_width="wrap_content"        
    android:layout_height="wrap_content"        
    android:text="右对齐"        
    app:layout_constraintRight_toRightOf="parent"        
    app:layout_constraintTop_toTopOf="parent"/>    
    
    <Button        
    android:layout_width="wrap_content"        
    android:layout_height="wrap_content"        
    android:text="水平居中"        
    app:layout_constraintLeft_toLeftOf="parent"        
    app:layout_constraintRight_toRightOf="parent"/>   
    
     <Button       
     android:layout_width="wrap_content"        
    android:layout_height="wrap_content"        
    android:text="垂直居中"        
    app:layout_constraintBottom_toBottomOf="parent"        
    app:layout_constraintTop_toTopOf="parent"/>    
    
    <Button        
    android:layout_width="wrap_content"        
    android:layout_height="wrap_content"        
    android:text="底部对齐"       
     app:layout_constraintBottom_toBottomOf="parent"        
    app:layout_constraintLeft_toLeftOf="parent"/>   
    
     <Button       
     android:layout_width="wrap_content"       
     android:layout_height="wrap_content"       
     android:text="水平居中+垂直居中"        
    app:layout_constraintBottom_toBottomOf="parent"        
    app:layout_constraintLeft_toLeftOf="parent"        
    app:layout_constraintRight_toRightOf="parent"        
    app:layout_constraintTop_toTopOf="parent"/>
    
    </android.support.constraint.ConstraintLayout>
    

    其显示效果如下图所示:

    image

    上面例子中app:layout_constraintLeft_toLeftOf="parent" 表示view的左边对齐父布局的左边。

    app:layout_constraintRight_toRightOf="parent"则表示view的右边对齐父布局的右边。

    其他同理,就不一一说明。

    相对位置的属性
    下面是ConstraintLayout确定位置的属性:

    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
    

    这些属性的值即可以是parent,也可以是某个view的id

    居中显示
    如果一个view满足以下

      app:layout_constraintLeft_toLeftOf="parent"      
      app:layout_constraintRight_toRightOf="parent"
    

    即view的左边对齐父布局的左边,view的右边对齐父布局的右边,除非这个view的大小刚好充满整个父布局;否则的话,就是水平居中显示了。我们可以理解为有两个力,它们左右互搏,view只能给扯到中间了。

    再来两个例子,把上面的属性基本都涉及到了,看下估计就懂了,就不逐一说明了。

    • 例子一:水平方向的相对位置
      这里主要关注水平方式的属性即可。
      布局代码:
    <?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">    
    <Button
            android:id="@+id/btn_center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00f"
            android:text="水平参照物"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#f00"
            android:text="Left_toLeftOf"
            app:layout_constraintBottom_toTopOf="@id/btn_center"
            app:layout_constraintLeft_toLeftOf="@id/btn_center"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#0f0"
            android:text="Right_toLeftOf"
            app:layout_constraintBottom_toTopOf="@id/btn_center"
            app:layout_constraintRight_toLeftOf="@id/btn_center"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#0f0"
            android:text="Right_toRightOf"
            app:layout_constraintRight_toRightOf="@id/btn_center"
            app:layout_constraintTop_toBottomOf="@id/btn_center"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#f00"
            android:text="Left_toRightOf"
            app:layout_constraintLeft_toRightOf="@id/btn_center"
            app:layout_constraintTop_toBottomOf="@id/btn_center"/>
    </android.support.constraint.ConstraintLayout>
    

    显示效果如下:

    image
    • 例子二:竖直方向的相对位置
      这里主要关注竖直方向的属性即可。
      布局代码:
    <?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"> 
       <Button
            android:id="@+id/btn_center"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:background="#00f"
            android:text="竖直参照物"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#f00"
            android:text="Top_toTopOf"
            app:layout_constraintTop_toTopOf="@id/btn_center"
            app:layout_constraintRight_toLeftOf="@id/btn_center"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#0f0"
            android:text="Bottom_toTopOf"
            app:layout_constraintBottom_toTopOf="@id/btn_center"
            app:layout_constraintRight_toLeftOf="@id/btn_center"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#0f0"
            android:text="Top_toBottomOf"
            app:layout_constraintLeft_toRightOf="@id/btn_center"
            app:layout_constraintTop_toBottomOf="@id/btn_center"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#f00"
            android:text="Bottom_toBottomOf"
            app:layout_constraintLeft_toRightOf="@id/btn_center"
            app:layout_constraintBottom_toBottomOf="@id/btn_center"/>
    </android.support.constraint.ConstraintLayout>
    

    显示效果如下:

    image

    尺寸约束

    view中使用warp_content或者固定值等等是没有问题的。但是ConstraintLayout中不支持MATCH_PARENT这个值,如果需要实现跟MATCH_PARENT同样的效果,可以使用0dp来代替,其表示MATCH_CONSTRAINT,即适应约束。其跟MATCH_PARENT还是有区别的,后面的例子(宽高比那一小节)会提到。
    我们来看下例子:

    <?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"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/btn_center" 
           android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="wrap_content"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/> 
    
       <Button
            android:id="@+id/btn_1"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="具体数值:200dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/btn_center"/>
    
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="0dp(MATCH_CONSTRAINT)"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/btn_1"/>
    
    </android.support.constraint.ConstraintLayout>
    

    其效果如下:

    image

    宽高比

    ConstraintLayout中,还可以将宽定义成高的一个比例或者高定义成宽的比率。首先,需要将宽或者高设置为0dp(即MATCH_CONSTRAINT),即要适应约束条件。然后通过layout_constraintDimensionRatio属性设置一个比率即可。这个比率可以是一个浮点数,表示宽度和高度之间的比率;也可以是“宽度:高度”形式的比率。比如:

    <?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"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:text="-------------------宽高比2:1-------------------"
            app:layout_constraintDimensionRatio="2:1"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>
    </android.support.constraint.ConstraintLayout>
    

    这里将按钮的高度设置为宽度的一半了。如下图所示:

    image

    如果宽和高都设置为0dpMATCH_CONSTRAINT),那么layout_constraintDimensionRatio的值需要先加一个"W,"或者"H,"来表示约束宽度或高度。如下:

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="H,16:9"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    

    这里例子是说,首先宽度将满足父布局的约束,然后将按照16:9的比例设置高度。

    百分比宽高

    ConstraintLayout还能使用百分比来设置view的宽高。
    要使用百分比,宽或高同样要设置为0dpMATCH_CONSTRAINT)。
    然后设置以下属性即可:

    app:layout_constraintWidth_default="percent" //设置宽为百分比app:layout_constraintWidth_percent="0.3" //0到1之间的值或app:layout_constraintHeight_default="percent" //设置高为百分比app:layout_constraintHeight_percent="0.3" //0到1之间的值
    

    例子如下:

    <?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"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="宽50%"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintWidth_percent="0.5"/>
    </android.support.constraint.ConstraintLayout>
    

    效果如下:

    image

    位置偏向

    如果想让view的位置偏向某一侧,可以使用以下的两个属性来设置:

    layout_constraintHorizontal_bias  //水平偏向layout_constraintVertical_bias  //竖直偏向
    

    其值同样也是0到1之间。
    比如,以下例子为横向偏向左侧30%,默认的居中效果就是50%:

    <?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"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="左边偏向30%"
            app:layout_constraintHorizontal_bias="0.3"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>
    </android.support.constraint.ConstraintLayout>
    

    显示效果如下:

    image

    权重

    LinearLayout中可以设置权重,ConstraintLayout同样也有这玩意。
    通过设置以下两个属性:

    app:layout_constraintHorizontal_weight //水平权重
    app:layout_constraintVertical_weight //竖直权重
    

    然后将相连的view两两约束好即可。如下:

    <?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"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:id="@+id/btn_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="权重为1"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@id/btn_2"/>
        <Button
            android:id="@+id/btn_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="权重为2"
            app:layout_constraintHorizontal_weight="2"
            app:layout_constraintLeft_toRightOf="@id/btn_1"
            app:layout_constraintRight_toLeftOf="@id/btn_3"/>
        <Button
            android:id="@+id/btn_3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintHorizontal_weight="2"
            android:text="权重为2"
            app:layout_constraintLeft_toRightOf="@id/btn_2"
            app:layout_constraintRight_toRightOf="parent"/>
    </android.support.constraint.ConstraintLayout>
    

    显示效果如下:

    image

    上面这种将相连的view两两约束好的实际上就形成了链。在ConstraintLayout中可以实现各种不同的链,权重链是其中一种。整个链由链中的第一个view(链头)上设置的属性控制。

    官网上一共有5种样式的链:

    image

    可以通过以下属性来设置链的样式:

    app:layout_constraintHorizontal_chainStyle="spread|spread_inside|packed"
    

    下面简单说下上面5种样式的实现:

    1. Spread Chain
      默认样式就是spread,所以可以不用设置。然后宽或高非0即可。
      效果如下图:
    image
    所以:Spread Chain = spread + 宽或高非0
    
    1. Spread Inside Chain
      这里设置以下属性
    app:layout_constraintHorizontal_chainStyle="spread_inside"
    

    然后宽度同样为非0,效果如下:

    image

    所以:Spread Inside Chain = spread_inside + 宽或高非0

    1. Weighter Chain
      即权重链,具体可以查看上一小节。
      Weighter Chain = spread + 宽或高为0 + 权重值

    2. Packed Chain
      Packed是一种view聚拢起来的效果,这里设置以下属性

    app:layout_constraintHorizontal_chainStyle="packed"
    

    宽度同样为非0,效果如下:

    image

    所以:Packed Chain = packed + 宽或高非0

    1. Packed Chain with bias
      就是Packed Chain再加一个偏向属性,偏向属性可以看下前面的内容。
      效果如下图所示:

      image

      所以:Packed Chain with bias = Packed Chain + bias

    Guideline辅助线

    Guideline可以用来辅助布局,通过Guideline能创建出一条条的水平线或者垂直线,该线不会显示到界面上,但是能够利用这些线条来添加约束去完成界面的布局。
    Guideline主要的属性有:

        android:orientation="horizontal|vertical"
         app:layout_constraintGuide_begin="30dp"
         app:layout_constraintGuide_end="30dp"
        app:layout_constraintGuide_percent="0.5"
    

    android:orientation=”horizontal|vertical”表示是水平或垂直引导线。

    app:layout_constraintGuide_begin=”30dp”,如果是水平引导线,则距离布局顶部30dp,如果是垂直引导线,则距离布局左边30dp。

    app:layout_constraintGuide_end=”30dp”,如果是水平引导线,则距离布局底部30dp,如果是垂直引导线,则距离布局右边30dp。

    app:layout_constraintGuide_percent=”0.5”,如果是水平引导线,则距离布局顶部为整个布局高度的50%,如果是垂直引导线,则距离布局左边文这个布局宽度的50%。

    来看个例子:

    <?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"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.constraint.Guideline
            android:id="@+id/guideline_h"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.5"/>
        <android.support.constraint.Guideline
            android:id="@+id/guideline_v" 
           android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.5"/>
        <Button
            app:layout_constraintLeft_toLeftOf="@id/guideline_v"
            app:layout_constraintTop_toTopOf="@id/guideline_h"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="辅助线定位"/>
    </android.support.constraint.ConstraintLayout>
    

    如下图所示:

    image

    本节基本把ConstraintLayout的常用属性都介绍了一遍了。通过使用ConstraintLayout能够减少布局嵌套等,可以有效的提升性能。

    相关文章

      网友评论

          本文标题:[Android]带你了解 Android 约束布局 Const

          本文链接:https://www.haomeiwen.com/subject/osmbnftx.html