ConstraintLayout(约束布局)

来源:互联网 发布:表达知错的句子 编辑:程序博客网 时间:2024/05/31 11:04

ConstraintLayout(约束布局)

使用ConstraintLayout需要我们添加如下依赖:
compile ‘com.android.support.constraint:constraint-layout:1.0.0-alpha4’

  • View在布局中相对于其他元素的位置是怎样的

    layout_constraint[源瞄点]_[目标锚点]=”[目标id]”

    app:layout_constraintBottom_toBottomOf="@+id/View1"
    该控件底部和View1的底部对齐

  • 偏置约束(Biasing Constraints)

    //偏离父容器左边缘的距离为父容器宽度的25%    app:layout_constraintHorizontal_bias="0.25"    //偏离父容器上边缘的距离为父容器高度的25%    app:layout_constraintVertical_bias="0.25" />
  • 锚向指示线(Anchoring to Guidelines)
    当需要一个任意位置的锚点时,我们可以使用指示线(guideline)。指示线实际上是View的子类,加入布局的方式也和普通View一样。指示线有如下特殊属性:

    它们的测量宽高总是0;
    它们的可见性总是View.GONE。
    指示线的存在只是为了为其他View定义一个水平或垂直的锚点

<ConstraintLayout    xmlns:android="..."  xmlns:app="..."  android:id="@+id/constraintLayout"  android:layout_width="match_parent"  android:layout_height="match_parent">  <android.support.constraint.Guideline    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:id="@+id/guideline"    android:orientation="vertical"    app:layout_constraintGuide_begin="72dp" />  <Button    android:id="@+id/button_cancel"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    app:layout_constraintStart_toStartOf="@+id/guideline"    app:layout_constraintTop_toTopOf="@+id/constraintLayout"    app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"    app:layout_constraintVertical_bias="0.25" />  <Button    android:id="@+id/button_next"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    app:layout_constraintStart_toStartOf="@+id/guideline"    app:layout_constraintTop_toTopOf="@+id/constraintLayout"    app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"    app:layout_constraintVertical_bias="0.75" /></ConstraintLayout>

这里写图片描述

Guideline可以使用以下三个属性之一:layout_constraintGuide_begin:指示线距离父容器左边缘的绝对距离layout_constraintGuide_end:指示线距离父容器右边缘的绝对距离layout_constraintGuide_Percent:指示线距离父容器左边缘的距离,这个属性的值是一个百分比,表示距离占父容器宽度的比例
  • View的尺寸

    1. 指定确切值 将layout_width或layou_height设为一个非零尺寸值(xx dp)即可
    2. 使View的尺寸正好“包裹”子View的内容 layout_width或layou_height设为wrap_content
    3. 填充父容器剩余空间 layout_width或layout_heigth设为0dp即可。ConstrainLayout不支持match_parent

    设定比例

    一个常规的UI布局需求就是把一个View的尺寸设为特定的宽高比。对于图片来说这个需求更是常见,比如将图片的宽高比设备1:1, 4:3, 16:9等等。通过使用ConstraintLayout,我们无需再创建一个自定义View来实现这个效果,只需使用layout_constraintDimensionRatio属性即可。

    使用这个属性时,我们需要固定View的宽或是高。假如我们固定了View的宽,并为其设置了一个宽高比,View的高就会在这个宽高比的约束下随着View的宽变化而变化。使用示例如下:

<ImageView    android:layout_width="0dp"    android:layout_height="wrap_content"    android:src="@drawable/grass"    app:layout_constraintDimensionRatio="4:3"    app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"    app:layout_constraintRight_toRightOf="@+id/constraintLayout"    app:layout_constraintBottom_toBottomOf="@+id/constraintLayout" />
  • app:layout_constraintBaseline_toBaselineOf
    表示此控件与某个控件水平对齐

这里写图片描述

  • app:layout_editor_absoluteX / app:layout_editor_absoluteY
    表示此控件在布局中X轴或Y轴的绝对坐标点。如图代码3

  • 目标消失后的margin

app:layout_goneMarginLeft//The left margin to use when the target is gone.app:layout_goneMarginTop//The top margin to use when the target is gone.app:layout_goneMarginRight//The right margin to use when the target is goneapp:layout_goneMarginBottom//The bottom margin to use when the target is gone.