Android ConstrainLayout布局中View位置的介绍与使用

news2024/9/20 0:49:23

一、介绍

        ConstrainLayout是一款布局View,再Design库中,现已被大家广泛接受并使用。ConstrainLayout的布局采用的方式和其他都不同,他的对其方式是类似RelativeLayout,但是和RelativeLayout有明显的区别。

        在布局渲染的时候,ConstrainLayout的子View是通过在一个容器中找到自己的位置,通过位置和对其方式来固定,所以在布局优化中,尝尝被提起到。

二、布局的对其方式

        好多小伙伴在使用的时候,发现ConstrainLayout的对其并不想RelativeLayout那么好用。想居父控件中间或底部已无法通过android:layout_gravity或者android:layout_alignParentRight来完成;

    <attr name="gravity">
        <!-- Push object to the top of its container, not changing its size. -->
        <flag name="top" value="0x30" />
        <!-- Push object to the bottom of its container, not changing its size. -->
        <flag name="bottom" value="0x50" />
        <!-- Push object to the left of its container, not changing its size. -->
        <flag name="left" value="0x03" />
        <!-- Push object to the right of its container, not changing its size. -->
        <flag name="right" value="0x05" />
        <!-- Place object in the vertical center of its container, not changing its size. -->
        <flag name="center_vertical" value="0x10" />
        <!-- Grow the vertical size of the object if needed so it completely fills its container. -->
        <flag name="fill_vertical" value="0x70" />
        <!-- Place object in the horizontal center of its container, not changing its size. -->
        <flag name="center_horizontal" value="0x01" />
        <!-- Grow the horizontal size of the object if needed so it completely fills its container. -->
        <flag name="fill_horizontal" value="0x07" />
        <!-- Place the object in the center of its container in both the vertical and horizontal axis, not changing its size. -->
        <flag name="center" value="0x11" />
        <!-- Grow the horizontal and vertical size of the object if needed so it completely fills its container. -->
        <flag name="fill" value="0x77" />
        <!-- Additional option that can be set to have the top and/or bottom edges of
             the child clipped to its container's bounds.
             The clip will be based on the vertical gravity: a top gravity will clip the bottom
             edge, a bottom gravity will clip the top edge, and neither will clip both edges. -->
        <flag name="clip_vertical" value="0x80" />
        <!-- Additional option that can be set to have the left and/or right edges of
             the child clipped to its container's bounds.
             The clip will be based on the horizontal gravity: a left gravity will clip the right
             edge, a right gravity will clip the left edge, and neither will clip both edges. -->
        <flag name="clip_horizontal" value="0x08" />
        <!-- Push object to the beginning of its container, not changing its size. -->
        <flag name="start" value="0x00800003" />
        <!-- Push object to the end of its container, not changing its size. -->
        <flag name="end" value="0x00800005" />
    </attr>

为什么?

这是因为ConstrainLayout已不采用这一套,而是通过自定义的位置来遍历view的位置。

所以针对常用的特色位置我已整理出来,可供参考

常见子View在父控件ConstrainLayout的位置设置

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="左上角"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="顶部居中"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="右上角"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="左边垂直居中"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"

        android:text="居父控件中间"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="右边垂直居中"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="左边底部垂直居底"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="底部居中"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="右边底部垂直居底"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

layout xml 效果图

通过以上大家可以很直观的看到View在parentView中该如何设置。

三、其他对其API的详解

待定

四、总结

其实位置就是通过各种对其方式进行固定,最后将view的坐标固定在父控件中。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/709351.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

获取Xilinx FPGA芯片IDCODE的4种方法(支持任何FPGA型号)

文章目录 方式1&#xff1a;官方文档方式2&#xff1a;一个头文件方式3&#xff1a;BSDL文件方法4&#xff1a;芯片IDCODE在线搜索网站Xilinx FPGA部分型号IDCODE汇总 方式1&#xff1a;官方文档 对于常用的Spartan-6系列可以在UG380文档中找到对应的IDCODE&#xff0c;Sparta…

CM+CDH 构建企业大数据平台

&#x1f4e2;&#x1f4e2;&#x1f4e2;&#x1f4e3;&#x1f4e3;&#x1f4e3; 哈喽&#xff01;大家好&#xff0c;我是【IT邦德】&#xff0c;江湖人称jeames007&#xff0c;10余年DBA及大数据工作经验 一位上进心十足的【大数据领域博主】&#xff01;&#x1f61c;&am…

电磁阀位、通、开/闭原理精髓

一、引用 电磁阀在液/气路系统中&#xff0c;用来实现液路的通断或液流方向的改变&#xff0c;它一般具有一个可以在线圈电磁力驱动下滑动的阀芯&#xff0c;阀芯在不同的位置时&#xff0c;电磁阀的通路也就不同。 阀芯在线圈不通电时处在甲位置&#xff0c;在线圈通电时处在…

ChatGPT助力校招----面试问题分享(八)

1 ChatGPT每日一题&#xff1a;有源和无源滤波器 问题&#xff1a;有源和无源滤波器的区别 ChatGPT&#xff1a;有源滤波器和无源滤波器是指使用不同的电路元件来实现滤波功能的电路 有源滤波器使用了一个或多个有源元件&#xff0c;例如晶体管、运算放大器等&#xff0c;以…

浅浅总结一下雅思听力技巧

1. 地图题 读题步骤要明确 &#xff08;1&#xff09;看图&#xff0c;要看看题目中是否有东南西北的标志&#xff0c;如果有的话&#xff0c;那么大概率题目中就会用到。同时也标记好左右的标志&#xff0c;防止考试的时候太紧张分不清。 弄清楚个元素的相对位置&#xff0…

华为OD机试真题 Python 实现【开心消消乐】【2023 B卷 100分】,附详细解题思路

目录 一、题目描述二、输入描述三、输出描述四、Python算法源码五、效果展示1、输入2、输出3、说明 一、题目描述 给定一个N行M列的二维矩阵&#xff0c;矩阵中每个位置的数字取值为0或1。矩阵示例如&#xff1a; 1 1 0 0 0 0 0 1 0 0 1 1 1 1 1 1 现需要将矩阵中所有的1进行…

卷积神经网络--猫狗系列之构建模型【ResNet50】

在上一期&#xff1a;卷积神经网络--猫狗系列之下载、导入数据集&#xff0c;如果测试成功就说明对数据的预处理工作已经完成&#xff0c;接下来就是构建模型阶段了&#xff1a; 据说建立一个神经网络模型比较简单&#xff0c;只要了解了各层的含义、不同层之间参数的传递等等&…

leetcode 1232. 缀点成线

题目描述解题思路执行结果 leetcode 1232. 缀点成线 题目描述 缀点成线 给定一个数组 coordinates &#xff0c;其中 coordinates[i] [x, y] &#xff0c; [x, y] 表示横坐标为 x、纵坐标为 y 的点。请你来判断&#xff0c;这些点是否在该坐标系中属于同一条直线上。 示例 1&a…

进程的调度常用算法

目录 先来先服务&#xff08;FCFS&#xff09;调度算法 短作业优先&#xff08;SJF&#xff09;的调度算法 基于时间片的轮转调度&#xff08;RR&#xff09;算法 先来先服务&#xff08;FCFS&#xff09;调度算法 系统将按照作业到达的先后次序来进行作业调度&#xff0c;或…

RT-Thread 5.0.1 qemu-virt64-aarch64 解决网络问题

参考文章 qemu 源码编译 qemu-system-aarch64 的方法 RT-Thread 5.0.1 qemu-virt64-aarch64 解决编译问题 前言 最近需要使用 RT-Thread qemu-virt64-aarch64&#xff0c;验证 aarch64 平台&#xff0c;也就是 ARM64 平台的一些网络功能&#xff0c;需要开启 qemu-virt64-aa…

UE4/5用贴图和GeneratedDynamicMeshActor曲面细分与贴图位移制作模型

目录 制作逻辑&#xff1a; ​编辑 曲面细分函数&#xff1a; 添加贴图逻辑&#xff1a; 代码&#xff1a; 制作逻辑&#xff1a; 在之前的文章中&#xff0c;我们使用了网格细分&#xff0c;而这一次我们将使用曲面细分函数&#xff0c;使用方法和之前是一样的&#xff1a…

高精度定位|RTK定位模块常见应用领域_厘米级室外定位解决方案

在室外场景&#xff0c;北斗、GPS等GNSS定位技术在持续的演变&#xff0c;精度越来越高&#xff0c;应用面也越来越广。随着新基建热潮的到来&#xff0c;借助5G新基建&#xff0c;无人驾驶、自动驾驶等技术正在逐步完善&#xff0c;对于定位的需求已经不仅仅只是粗略的轨迹&am…

uniapp-ios打包步骤

前置条件&#xff1a;已申请Apple ID并注册Apple Developer Program 一、登录苹果开发者官网 登录&#xff1a;https://developer.apple.com/ 点击账户&#xff08;Account&#xff09;进行登录&#xff0c;登录成功出现如下页面 二、创建证书、标识符、描述文件等 点击…

vue3基本指令使用

<script setup lang"ts"> import {ref} from vue //响应式数据 const num: number 1 const arr1: number[] [1, 2, 3, 4, 5] const str: string "我是一段文字" const htmlstr: string <section style"color:red">我是一个secti…

微服务-基于Docker安装Sentinel

目录 1、拉取Sentinel镜像 2、构建Sentinel容器 3、访问Sentinel 1、拉取Sentinel镜像 代码&#xff1a; docker pull bladex/sentinel-dashboard:1.8.0 实例&#xff1a; rootlocalhost howlong]# docker pull bladex/sentinel-dashboard:1.8.0 1.8.0: Pulling from blade…

集合及Collection集合

1&#xff1a;集合特点&#xff1a; 集合的大小不确定&#xff0c;启动后可以动态变化&#xff0c;类型也可以选择不固定。集合更像气球集合非常适合做元素的增删操作注意&#xff1a;集合只能存储引用类型数据&#xff0c;如果要存储基本类型数据可以选用包装类。 2&#xf…

2.FreeRTOS系统任务调度简介及任务状态

目录 一、基础知识 1、FreeRTOS 任务状态 (1)运行态 (2)就绪态 (3) 阻塞态 (4) 挂起态 二、任务调度简介 1.抢占式调度 2.时间片调度 3.协程式调度 一、基础知识 1、FreeRTOS 任务状态 FreeRTOS 中任务存在四种任务状态&#xff0c;分别为运行态、就绪态、阻塞态和挂…

Word模板替换,并转PDF格式输出,Linux服务器乱码问题解决

Poi-tl参考文档地址&#xff1a;http://deepoove.com/poi-tl/1.8.x/#hack-loop-table word模板替换&#xff0c;转pdf 1. 依赖引入&#xff1a;2. word模板配置&#xff1a;3. 示例demo:4 . 效果图5. 本地测试没问题&#xff0c;上Linux服务乱码&#xff0c;出现小方框 1. 依赖…

Python———运行环境搭建

不管用什么工具开发 Python 程序&#xff0c;都必须安装 Python 的运行环境。 目前最常用的是Windows 、 Linux 平台。这里 我们以Windows10为主讲解。 其实编程和平台关系不大。大家也可以使用Linux、Mac。 Windows 平台下 Python 环境搭建 第一步&#xff1a;进入 python 官…

python绘制热力图,数据来源pandas.dataframe

通过pandas.dataframe绘制热力图&#xff0c;并标出颜色最深即z轴数据最大的点。 import matplotlib.pyplot as plt import pandas as pd import numpy as npclass Heatmap:def __init__(self, data, marker*, marker_colorwhite, marker_size10):self.data dataself.size l…