知识回顾
Android 五大布局:
- 线性布局
- 相对布局
- 帧布局
- 网格布局
- 绝对布局
网格布局几乎没有用到过,绝对布局如果不是 Webview 继承与他,基本也被废弃了。
定义
Android第六大布局,约束布局,一定程度替代了前五大布局。
使用原则:
线性布局的效率还是值得肯定的,在不嵌套的前提下,如果用鲜血布局更简单还是推荐使用;完全代替相对布局。
入门使用
约束布局必须对当前view添加约束条件才能确认他的位置,至少在横向和纵向各添加一个约束条件
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingVertical="20dp"
android:textColor="#FF2D2942"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="提示" />
</androidx.constraintlayout.widget.ConstraintLayout>
例:
app:layout_constraintStart_toStartOf
layout_constraint:代表这是一个约束属性
app:layout_constraintStart_toStartOf
Start:代表当前这个控件的开始位置
app:layout_constraintStart_toStartOf
toStartOf:代表要约束到的控件的开始位置
下划线后表示目标控件
app:layout_constraintStart_toStartOf的value可以是父布局,也可以是具体的某一个view
所以:
app:layout_constraintStart_toStartOf=“parent”
意思是:我的开始的位置约束到父布局的开始位置
同样的道理:
app:layout_constraintEnd_toEndOf=“parent”
我的结束的位置约束到父布局的结束位置
app:layout_constraintTop_toTopOf=“parent”
我的头部的位置约束到父布局的头部位置
app:layout_constraintBottom_toBottomOf=“parent”
我的底部的位置约束到父布局的底部位置
然后排列组合:
app:layout_constraintStart_toEndOf=“parent”
我的开始的位置约束到父布局的结束位置
,,,,,,