首页 > 其他分享 >ConstraintLayout解析

ConstraintLayout解析

时间:2023-06-06 23:56:12浏览次数:44  
标签:layout button 布局 ConstraintLayout constraintStart 对齐 解析

@

目录

1.前言

你是不是一直不敢用ConstraintLayout,是以为属性太多太复杂?你心理上的惰性,畏惧它。它其实很好用很强大,如果要用就需要一个入门的敲门砖

2.了解ConstraintLayout

特点:简化操作、解决布局嵌套、自适应布局、可百分比布局、可同时替代线性布局LinearLayout和相对布局RelativeLayout的强大功能

3.基本用法

ConstraintLayout的位置约束属性如下,属性值可以是parent或者@+id/兄弟组件的id:
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_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
Layout_constraintEnd_toEndOf
怎么这么多12个属性,相对布局你都会,这个就不用害怕理解一下就明白了,相对布局不就是top,bottom,left,right四个属性吗,看单词只不过多了start和end(为了支持阿拉伯国家),这里你也可以当成left和right,效果一样。推荐使用start和end.下面就解析一下这些属性。

3.1 看一个布局

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

在这里插入图片描述
先理解一下这个button控件,它使用了layout_constraintStart_toStartOf、layout_constraintBottom_toBottomOf和
layout_constraintTop_toTopOf
意思:parent表示父布局,而且使用约束布局必须在水平和垂直都添加约束,不然不完整。
button的左边和父布局的左边对齐,
button的下面边和父布局的下面边对齐,
button的上面的边和父布局的上面边对齐
从显示效果图可以看到第一个左对左效果有了,又由于要求控件上对上和下对下就会出现在中间的效果。

3.2再看一个布局

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:testSize="24sp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:testSize="24sp"
        android:text="Button1"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toTopOf="@+id/button"
     />

</androidx.constraintlayout.widget.ConstraintLayout>

在这里插入图片描述
它使用了layout_constraintStart_toEndOf和
layout_constraintTop_toTopOf
意思:"@+id/button表示兄弟布局
button1的左边和兄弟布局的右边对齐,
button1的上面边和兄弟布局的上面边对齐,
从显示效果图可以看到效果都有了,真的好强大,下面再讲布局的时候不用详细讲了,不用死记硬背,理解了这个就基本的会了。
此博客没完成,有时间继续。尽快会完成

标签:layout,button,布局,ConstraintLayout,constraintStart,对齐,解析
From: https://www.cnblogs.com/wzqnxd/p/17462112.html

相关文章

  • Spring 学习笔记(10)—— 视图和视图解析器
    1认识视图2认识视图解析器3使用JSP和JSTL4模板视图FreeMarkerFreeMarker仅负责基于模板对模型数据进行渲染的工作在SpringWeb上下文中配置FreeMarker......
  • 正则解析案例01__网页图片爬取
    趣图地址:https://www.gxt8.cn/gxtp01/1.正则表达式提取网页图片源码: 2.提取页面所有的图片地址后续将其拼接: 3.获取图片二进制_定义图片存储路径_图片二进制写入图片路径: ......
  • 特殊文件:XML的解析
            ......
  • 概念名词解析
    一. QT是什么   -C++的库封装C++的框架(QTCompany)类似微软的MFC(封装的C++)有什么用-方便使用C++,能方便进行C++的可视化开发,本质上还是调用QT库生成界面好处:跨平台(LinuxWindows) 二.MESMES(英文ManufacturingExecutionSystem的缩写),即生产执行系统,是近几年发展......
  • 《深度剖析CPython解释器》19. Python类机制的深度解析(第三部分): 自定义类的底层实
    https://www.cnblogs.com/traditional/p/13593927.html楔子Python除了给我提供了很多的类之外,还支持我们定义属于自己的类,那么Python底层是如何做的呢?我们下面就来看看。自定义class老规矩,如果想知道底层是怎么做的,那么就必须要通过观察字节码来实现。classGirl:nam......
  • Json解析字符串报错syntax error, expect {, actual string, pos 0, fastjson-version
    ExpectedBEGIN_OBJECTbutwasSTRINGatline1column2path$syntaxerror,expect{,actualstring,pos0,fastjson-version1.2.62syntaxerror,expect{,actualstring,pos0,fastjson-version1.2.62以上的报错都是Json字符串格式错误,比如缺少{},比如两头多了......
  • Python生成器深度解析:构建强大的数据处理管道
    前言生成器是Python的一种核心特性,允许我们在请求新元素时再生成这些元素,而不是在开始时就生成所有元素。它在处理大规模数据集、实现节省内存的算法和构建复杂的迭代器模式等多种情况下都有着广泛的应用。在本篇文章中,我们将从理论和实践两方面来探索Python生成器的深度用法。生......
  • Python生成器深度解析:构建强大的数据处理管道
    前言生成器是Python的一种核心特性,允许我们在请求新元素时再生成这些元素,而不是在开始时就生成所有元素。它在处理大规模数据集、实现节省内存的算法和构建复杂的迭代器模式等多种情况下都有着广泛的应用。在本篇文章中,我们将从理论和实践两方面来探索Python生成器的深度用法。......
  • RTP协议之Header结构解析(转)
    原文:https://blog.csdn.net/yu_yuan_1314/article/details/9849581作者:MultiMedia之旅 实时传输协议RTP,RTP提供带有实时特性的端对端数据传输服务,传输的数据如:交互式的音频和视频。那些服务包括有效载荷类型定义,序列号,时间戳和传输监测控制。应用程序在UDP上运行RTP来使......
  • python解析
    关于beautifulsoupBeautifulSoup用来从HTML或XML文件中提取数据现在最新的版本是beautifulsoup3已经停止开发,现在最新推荐使用的是beautifulsoup4安装pipinstallbeautifulsoup4beautifulsoup要用起来还需要一个解析器的东西,官网列出了主要的一些解析器除了第一个标......