android ConstraintLayout约束布局的使用

简介

为什么要使用ConstraintLayout?

  1. 为了在Android应用布局中保持扁平的层次结构,减少布局的嵌套
  2. 为应用创建响应快速而灵敏的界面
  3. 使用比RelativeLayout更灵活,性能更出色
  4. 可以按照比例约束控件位置和尺寸,能够更好地适配屏幕大小不同的机型

既然ConstraintLayout的优点这么大,但是它的缺点我们也一点要了解,才能在开发中更加灵活的运用。缺点:

  1. 删除某个控件会引起其他关联此id控件都收到影响

导入依赖

implementation 'com.android.support.constraint:constraint-layout:1.1.3'

布局属性

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

使用:

app:layout_constraintBaseline_toBaselineOf="@+id/tv_1"

居中

<!--水平居中-->
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
<!--垂直居中-->
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"

一行3个控件居中

在这里插入图片描述

<TextView
        android:id="@+id/TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/TextView2" />

    <TextView
        android:id="@+id/TextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/TextView1"
        app:layout_constraintRight_toLeftOf="@+id/TextView3"
        app:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:id="@+id/TextView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/TextView2"
        app:layout_constraintRight_toRightOf="parent" />

3个TextView相互约束,两端两个TextView分别与parent约束,成为一条链

在某个控件右边

 app:layout_constraintLeft_toRightOf="@+id/TextView1"

在某个控件下面

app:layout_constraintTop_toBottomOf="@+id/TextView1" 

两个居中的控件贴合一起

app:layout_constraintHorizontal_chainStyle="packed":将两个控件水平排列,并将它们紧密地包装在一起。
app:layout_constraintVertical_chainStyle="packed":将两个控件垂直排列,并将它们紧密地包装在一起。
app:layout_constraintHorizontal_bias="0":将第一个控件的左边缘与父布局的左边缘对齐。
app:layout_constraintVertical_bias="0":将第一个控件的顶部边缘与父布局的顶部边缘对齐。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Android Studio的约束布局是一种强大而灵活的布局方式,它允许您以声明性的方式定义视图之间的关系。以下是使用约束布局的一些基本步骤: 1. 在布局文件中使用`ConstraintLayout`作为根视图。在XML文件中,您可以这样声明一个约束布局: ``` <androidx.constraintlayout.widget.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"> <!-- 在这里添加其他视图 --> </androidx.constraintlayout.widget.ConstraintLayout> ``` 2. 定义视图之间的约束关系。您可以通过拖动和放置视图来在设计编辑器中设置约束,也可以在XML文件中手动编写约束。例如,要将一个按钮位于父布局顶部,并与左右边缘有10dp的间距,您可以这样定义约束: ``` <Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="My Button" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_marginStart="10dp" app:layout_marginEnd="10dp"/> ``` 3. 可选地,您还可以使用链(chains)和辅助对象(guidelines)来进一步定义视图之间的关系,以及在不同屏幕尺寸下的自适应布局约束布局的优势在于它可以适应各种屏幕尺寸和方向,并且可以减少嵌套布局的需要。您可以通过在Android Studio的设计编辑器中直观地操作视图和约束,或者手动编辑XML文件来创建约束布局。要了解更多关于约束布局的信息和用法,请参阅Android官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wy313622821

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值