Android约束布局ConstraintLayout

2.为什么要用ConstraintLayout

3.ConstraintLayout的简单使用

1.ConstraintLayout是什么

ConstraintLayout 是Google在2016年推出的一种布局,是一个ViewGroup它的出现主要是为了解决布局嵌套过多的问题,以灵活的方式定位和调整小部件。

2.为什么要用ConstraintLayout

可以有效地解决布局嵌套过多的问题(设备绘制视图所需的时间和计算功耗)

性能比较结果表明: ConstraintLayout在测量/布局阶段的性能比相对布局大约高40%

3.ConstraintLayout的简单使用

以前在新建一个项目后,发现已经默认是ConstraintLayout布局了,如下:

在这里插入图片描述

ConstraintLayout的基本用法也很简单,比如我们想要向布局中添加一个按钮,那么只需要从左侧的Palette区域一个Button进去就可以了,如下图所示。

在这里插入图片描述

然后会发现xml代码中Button标签爆红

在这里插入图片描述

这句话的意思是:此视图不受约束。它只有设计时的位置,所以它将在运行时跳到(0.0),除非你添加更多的约束Ctrl+F1)。

那么接下来就是添加约束了:(设置参照物)

在这里插入图片描述

(通过四个点来确定其布局)首先来个最简单的,上下左右都拉一次。

在这里插入图片描述

然后就不爆红了,这就是最基础的使用了(这时通过鼠标可以直接移动控件的位置),下面通过一个小Demo,来拓展一下

在这里插入图片描述

layout_constraintStart_toEndOf :同left_toRightOf

layout_constraintStart_toStartOf :同left_toLeftOf

layout_constraintEnd_toStartOf :同right_toLeftOf

layout_constraintEnd_toEndOf :同right_toRightOf

layout_constraintLeft_toLeftOf :当前View的左侧和另一个View的左侧位置对齐

layout_constraintLeft_toRightOf :当前view的左侧会在另一个View的右侧位置

layout_constraintRight_toLeftOf :当前view的右侧会在另一个View的左侧位置

layout_constraintRight_toRightOf :当前View的右侧和另一个View的右侧位置对齐

layout_constraintTop_toTopOf :头部对齐

layout_constraintTop_toBottomOf :当前View在另一个View的下侧

layout_constraintBottom_toTopOf :当前View在另一个View的上方

layout_constraintBottom_toBottomOf :底部对齐

layout_constraintBaseline_toBaselineOf :文字底部对齐

Demo代码如下:

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

tools:context=“.MainActivity”>

<EditText

android:id=“@+id/main_et_user”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginStart=“32dp”

android:layout_marginLeft=“32dp”

android:layout_marginTop=“8dp”

android:layout_marginEnd=“8dp”

android:layout_marginRight=“8dp”

android:layout_marginBottom=“8dp”

android:ems=“10”

android:inputType=“textMultiLine”

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintEnd_toEndOf=“parent”

app:layout_constraintHorizontal_bias=“0.493”

app:layout_constraintStart_toStartOf=“parent”

app:layout_constraintTop_toTopOf=“parent”

app:layout_constraintVertical_bias=“0.164” />

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginStart=“8dp”

android:layout_marginLeft=“8dp”

android:layout_marginTop=“8dp”

android:layout_marginEnd=“8dp”

android:layout_marginRight=“8dp”

android:layout_marginBottom=“8dp”

android:text=“密 码:”

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintEnd_toEndOf=“parent”

app:layout_constraintHorizontal_bias=“0.079”

app:layout_constraintStart_toStartOf=“parent”

app:layout_constraintTop_toTopOf=“parent”

app:layout_constraintVertical_bias=“0.185” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginStart=“8dp”

android:layout_marginLeft=“8dp”

android:layout_marginTop=“8dp”

android:layout_marginEnd=“8dp”

android:layout_marginRight=“8dp”

android:layout_marginBottom=“8dp”

android:text=“登录”

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintEnd_toEndOf=“parent”

app:layout_constraintHorizontal_bias=“0.224”

app:layout_constraintStart_toStartOf=“parent”

app:layout_constraintTop_toTopOf=“parent”

app:layout_constraintVertical_bias=“0.89” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginStart=“8dp”

android:layout_marginLeft=“8dp”

android:layout_marginTop=“8dp”

android:layout_marginEnd=“8dp”

android:layout_marginRight=“8dp”

android:layout_marginBottom=“8dp”

android:text=“注册”

app:layout_constraintBottom_toBottomOf=“parent”
先自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以扫码领取!!!!

Android高级架构师

由于篇幅问题,我呢也将自己当前所在技术领域的各项知识点、工具、框架等汇总成一份技术路线图,还有一些架构进阶视频、全套学习PDF文件、面试文档、源码笔记。

  • 330页PDF Android学习核心笔记(内含上面8大板块)

  • Android学习的系统对应视频

  • Android进阶的系统对应学习资料

  • Android BAT部分大厂面试题(有解析)

好了,以上便是今天的分享,希望为各位朋友后续的学习提供方便。觉得内容不错,也欢迎多多分享给身边的朋友哈。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可免费领取!

中…(img-OPZR9noz-1711285723683)]

  • Android BAT部分大厂面试题(有解析)

[外链图片转存中…(img-cJZQcu0z-1711285723683)]

好了,以上便是今天的分享,希望为各位朋友后续的学习提供方便。觉得内容不错,也欢迎多多分享给身边的朋友哈。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可免费领取!

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android约束布局(ConstraintLayout)是一种可以灵活控制子控件位置和大小的布局方式。它是Android官方在2016年Google的I/O大会推出的,并且在最新版的Android Studio中成为创建布局文件的默认根元素。\[1\] 在约束布局中,子控件的位置和大小是通过设置约束条件来实现的。例如,可以使用app:layout_constraintLeft_toLeftOf和app:layout_constraintTop_toTopOf属性来指定子控件相对于父布局的左边和顶部的位置。\[2\] 此外,约束布局还支持设置子控件的最小宽度(android:minWidth)、最小高度(android:minHeight)、最大宽度(android:maxWidth)和最大高度(android:maxHeight)。为了替代match_parent属性,官方推荐使用0dp(MATCH_CONSTRAINT)并结合约束条件来设置子控件的宽度和高度。\[3\] 下面是一个使用约束布局的示例代码: ```xml <?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"> <TextView android:id="@+id/TextView1" android:layout_width="0dp" android:layout_height="wrap_content" android:background="#E8C99B" android:gravity="center" android:text="textview1" android:textColor="@color/black" android:textSize="25sp" android:textStyle="bold" android:layout_marginLeft="100dp" android:layout_marginTop="100dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> ``` 这个示例中,TextView的宽度被设置为0dp(MATCH_CONSTRAINT),并且通过约束条件app:layout_constraintLeft_toLeftOf和app:layout_constraintTop_toTopOf来确定其位置。同时,还设置了最小宽度、最小高度、背景颜色等属性。 #### 引用[.reference_title] - *1* [【AndroidConstraintLayout约束布局最全解析](https://blog.csdn.net/huweiliyi/article/details/122894823)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Android——ConstraintLayout(约束布局)](https://blog.csdn.net/The_onion/article/details/127675500)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值