环境:
Android studio,jdk1.8,API 28,Android 9.0
接触缘由:
由eclipse改用Android studio,突然发现RelativeLayout被替换成了ConstrainLayout,有很多不习惯的地方,从头学习一下,也记录一下。
基础
名字:constraintLayout 约束布局
姓:命名空间 app
出生年/地:2006年谷歌I/O退出
年龄说明:在项目build.gradle中声明
受到约束:其他视图,父容器(parent),基准线(Guideline)
优点:降低布局的层级, 加快渲染速度,适合复杂布局
基本五官:
layout_constrant[本源位置]_[目标位置]="[目标ID]"
视觉效果:
app:layout_constraintBottom_toTopOf="@id/button1"
将当前view的底部对其到button1的顶部
eg:实现底部导航栏
<?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:id="@+id/constraintLayout">
<TextView
android:id="@+id/tab1"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="@color/colorPrimary"
android:gravity="center"
android:text="tab1"
android:textColor="@color/colorWhite"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/tab2" />
<TextView
android:id="@+id/tab2"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="tab2"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toRightOf=<