向ConstraintLayout迁移

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/knight1996/article/details/82814858

哎,前几天写的关于ConstraintLayout不小心写删掉了,今天重新写一篇。
ConstraintLayout是现在google力推的一种布局,它的特点就是可以做到几乎没有嵌套,提高UI渲染速度。

从 RelativeLayout向ConstraintLayout迁移

网上有人说ConstraintLayout是RelativeLayout增强版,确实RelativeLayout能做到的ConstraintLayout都能做到,和RelativeLayout相似的属性如下所示:
1.基础属性

layout_constraintLeft_toLeftOf   	//控件的左边位于另一个控件的的左边
layout_constraintLeft_toRightOf 	//控件的左边位于另一个控件的的右边
layout_constraintRight_toLeftOf 	//控件的右边边位于另一个控件的的左边
layout_constraintRight_toRightOf	//控件的右边位于另一个控件的的右边
layout_constraintTop_toTopOf 	//控件的顶部位于另一个控件的的顶部,一下属性都是类似意思
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf

在这里插入图片描述
上面布局如果使用RelativeLayout实现可能代码是这样的

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2"
        android:layout_toRightOf="@id/button1" />
</RelativeLayout>

让button2在button1的右边使用 android:layout_toRightOf即可,在ConstraintLayout中用app:layout_constraintLeft_toRightOf 来替换

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"
        app:layout_constraintLeft_toLeftOf="parent"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2"
        app:layout_constraintLeft_toRightOf="@+id/button1" />
</android.support.constraint.ConstraintLayout>

2.相对父容器方法
在RelativeLayout 还有相对父容器的属性,如android:layout_alignParentBottom 在ConstraintLayout还是使用上面的属性只要把属性对应的值设置为parent即可
3. 居中实现
在RelativeLayout 还有一些居中的方法,例如android:layout_centerHorizontal=“true” 就可以实现水平居中,在在RelativeLayout 还有一些居中的方法,例如android:layout_centerHorizontal=“true” 就可以实现水平居中,在ConstraintLayout中实现居中很有意思,就像物理学中的二力平衡似的。如下面代码就可以实现水平居中

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>

实现的主要是app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"
这两个实现了左右 “二力平衡” 实现水平居中,除了 “二力平衡” 当然也要 “非二力平衡” 的时候,使用这两个属性之后再使用app:layout_constraintHorizontal_bias=""这个属性设置水偏移量,对应值为[0,1].垂直居中和水平居中方法类似。

掌握上边属性基本上就可以从RelativeLayout迁移到ConstraintLayout
例如下面布局
在这里插入图片描述

实现代码如下

<?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">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/blue_rectangle"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="标题"
        android:textSize="14sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toRightOf="@id/imageView" />

    <TextView
        android:id="@+id/tv_comment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="评论"
        android:textColor="#000"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="@id/tv_title"
        app:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="内容内容"
        android:textSize="14sp"
        app:layout_constraintLeft_toRightOf="@id/imageView"
        app:layout_constraintTop_toBottomOf="@id/tv_title" />

    <TextView
        android:id="@+id/tv_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="10"
        android:textSize="14sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@id/imageView"
        app:layout_constraintLeft_toRightOf="@id/imageView" />
</android.support.constraint.ConstraintLayout>

从 LinearLayout向ConstraintLayout迁移

LinearLayout我觉得最好用的就是权重分配了,可以通过比例来分配空间ConstraintLayout中要想实现这种效果,要理解什么是链的概念
链就要做到 “互相拉着 ,你拉着我,我拉着你”如下面代码所示

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button1"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/button2" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button2"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toRightOf="@+id/button1"
        app:layout_constraintRight_toLeftOf="@id/button3" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button3"
        app:layout_constraintHorizontal_weight="2"
        app:layout_constraintLeft_toRightOf="@+id/button2"
        app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>

在这里插入图片描述

上面三个button互相约束形成链式,就可以使用app:layout_constraintHorizontal_weight属性设置权重,和LinearLayout相同设置水平权重宽度就没什么意义了所以宽度也是设置成0dp。

猜你喜欢

转载自blog.csdn.net/knight1996/article/details/82814858
今日推荐