创建一个空activity
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.Main2Activity">
</androidx.constraintlayout.widget.ConstraintLayout>
加入一个按钮,默认是在左上角
属性尝试
app:layout_constraintRight_toRightOf=”parent”
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#33f"
android:textColor="#fff"
android:text="Button"
app:layout_constraintRight_toRightOf="parent"
tools:ignore="MissingConstraints" />
按钮会被拉到右侧去(盒子的右侧(constraintRight)会被拉到父容器的右侧(toRightOf=”parent”))
按照这个逻辑猜测一下
app:layout_constraintLeft_toRightOf=”parent”
盒子的左侧会被拉到父容器的右侧,也就是移除了容器;
app:layout_constraintLeft_toLeftOf=”parent”
盒子左侧容器左侧
app:layout_constraintRight_toLeftOf=”parent”
盒子右侧容器左侧,被拉到左边容器外
上下左右基本约束
盒子的左右方向约束可以对应容器的左右方向
上下方向对应上下方向
所有约束属性
基本八个属性
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