创建一个空activity

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/container"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".ui.main.Main2Activity">
  9. </androidx.constraintlayout.widget.ConstraintLayout>

加入一个按钮,默认是在左上角

属性尝试

app:layout_constraintRight_toRightOf=”parent”

  1. <Button
  2. android:id="@+id/button2"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:background="#33f"
  6. android:textColor="#fff"
  7. android:text="Button"
  8. app:layout_constraintRight_toRightOf="parent"
  9. tools:ignore="MissingConstraints" />

按钮会被拉到右侧去(盒子的右侧(constraintRight会被拉到父容器的右侧(toRightOf=”parent”
image.png

按照这个逻辑猜测一下
app:layout_constraintLeft_toRightOf=”parent”
盒子的左侧会被拉到父容器的右侧,也就是移除了容器;
image.png

app:layout_constraintLeft_toLeftOf=”parent”
盒子左侧容器左侧
image.png

app:layout_constraintRight_toLeftOf=”parent”
盒子右侧容器左侧,被拉到左边容器外

上下左右基本约束

盒子的左右方向约束可以对应容器的左右方向
上下方向对应上下方向
image.png


所有约束属性

基本八个属性

layout_constraintLeft_toLeftOf :当前View的右侧和另一个View的右侧位置对齐,与RelativeLayout的alignLeft属性相似
layout_constraintLeft_toRightOf :当前view的左侧会在另一个View的右侧位置 与RelativeLayout的toRightOf属性相似
layout_constraintRight_toLeftOf :当前view的右侧会在另一个View的左侧位置 与RelativeLayout的toLeftOf属性相似
layout_constraintRight_toRightOf :当前View的右侧和另一个View的右侧位置对其,与RelativeLayout的alignRight属性相似
layout_constraintTop_toTopOf :头部对齐,与alignTop相似
layout_constraintTop_toBottomOf :当前View在另一个View的下侧 与below相似
layout_constraintBottom_toTopOf :当前View在另一个View的上方 与above相似
layout_constraintBottom_toBottomOf :底部对齐,与alignBottom属性相似

被废弃的四个

layout_constraintStart_toEndOf :同left_toRightOf
layout_constraintStart_toStartOf :同left_toLeftOf
layout_constraintEnd_toStartOf :同right_toLeftOf
layout_constraintEnd_toEndOf :同right_toRightOf

文字对齐

layout_constraintBaseline_toBaselineOf :文字底部对齐,与alignBaseLine属性相似

UI编辑器属性

下面几个属性是 UI 编辑器所使用的,用了辅助拖拽布局的,在实际使用过程中,可以不用关心这些属性。

layout_optimizationLevel
layout_editor_absoluteX
layout_editor_absoluteY
layout_constraintBaseline_creator
layout_constraintTop_creator
layout_constraintRight_creator
layout_constraintLeft_creator
layout_constraintBottom_creator

参考

https://blog.csdn.net/u012254541/article/details/80626951