约束布局属性学习

1、layout_constraintHorizontal_bias

layout_constraintHorizontal_bias 是 ConstraintLayout 中的一个重要属性,用于控制一个视图在父视图或相关视图中水平位置的偏移。这种偏移通过在0到1之间的浮点值来设置,0代表完全靠近左边或起始位置,1代表完全靠近右边或结束位置,0.5则居中。

属性解释
layout_constraintHorizontal_bias=“0.0” : 视图完全靠到父布局的左边界。
layout_constraintHorizontal_bias=“0.5” : 视图在左右边界之间完全居中。
layout_constraintHorizontal_bias=“1.0” : 视图完全靠到父布局的右边界。

注意事项

  • 使用 layout_constraintHorizontal_bias 时,需要同时设置视图的两侧约束(例如Start_toStartOf和End_toEndOf)。
  • 属性值不必是刚好0.0、0.5或1.0,您可以使用任何0到1之间的浮点数来实现不同程度的偏移。
    通过调整 layout_constraintHorizontal_bias,您可以灵活地控制视图在水平空间中的位置,以适应各种UI布局设计需求。

2、Guideline

Guideline 本身是不可见的,只作为一种虚拟边界来帮助对齐和定位其他视图。
可以为 Guideline 设置固定偏移(相对于父布局的起始或结束百分比)或者绝对偏移。

举例如下:
在这里插入图片描述

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

    <!-- 垂直 Guideline,位置为布局宽度的25% -->
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/verticalGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.25" />

    <!-- 水平 Guideline,距离顶部100dp -->
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/horizontalGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="100dp" />

    <!-- 使用 Guideline 对齐的视图 -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Aligned with Vertical Guideline"
        android:background="#FF5722"
        app:layout_constraintStart_toStartOf="@id/verticalGuideline"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Aligned with Horizontal Guideline"
        android:background="#3F51B5"
        app:layout_constraintTop_toTopOf="@id/horizontalGuideline"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

属性解释
垂直 Guideline:
android:orientation=“vertical”:设置为垂直指导线。
app:layout_constraintGuide_percent=“0.25”:位置占父布局宽度的25%。
水平 Guideline:
android:orientation=“horizontal”:设置为水平指导线。
app:layout_constraintGuide_begin=“100dp”:距离父布局顶部100dp的位置。

使用 Guideline 的好处
灵活性:可以通过调整 Guideline 的位置来全局修改多个元素的对齐。
可维护性:简化视图之间的约束定义,减少重复代码。
响应性:可以基于父容器的尺寸自动调整视图位置,增强布局的适配能力。

3、layout_constraintHorizontal_chainStyle

用于指定水平链样式的属性,主要的链样式包括:

spread(默认):视图之间均匀分布。
spread_inside: 视图之间保持大小和均匀分布,两端不等距。
packed: 视图集中在一起,可以通过设置偏移来调整链的位置。

1)、spread

在这里插入图片描述

2)、spread_inside

在这里插入图片描述

3)、packed

在这里插入图片描述

4)、代码:
<?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"
    tools:context=".MainActivity"
    tools:layout_editor_absoluteY="25dp">

    <!-- 链头 控件 中设置 Chain 风格 spread -->
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintEnd_toStartOf="@+id/button3"
        app:layout_constraintStart_toEndOf="@+id/button1" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button2" />

</androidx.constraintlayout.widget.ConstraintLayout>

4、layout_constraintHorizontal_weight

举例:

0dp 布局宽度:
在 ConstraintLayout 中,要启用权重,需要将 layout_width 设置为 0dp。这表示视图的宽度将由权重和约束确定,而不是自身内容或父布局。
权重的分配:
Button 1 和 Button 2 设置了权重为 1,因此它们将占据相同的宽度。
Button 3 设置了权重为 2,因此它将占用两个单位宽度,相较于其他按钮更宽。
注意事项:
1、链的创建: 确保视图之间通过合适的约束(如 Start_toEndOf 和 End_toStartOf)链接成链。这是使用 layout_constraintHorizontal_weight 的前提条件。
2、效果影响: 权重的实际大小并不重要,重要的是他们之间比例。例如,权重为 1:2:3 与 2:4:6 执行效果相同,因为比例一致。

<?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:id="@+id/button1"
        android:layout_width="0dp" <!-- 使用 0dp 来启用权重 -->
        android:layout_height="wrap_content"
        android:text="Button 1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/button2"
        app:layout_constraintHorizontal_weight="1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp" <!-- 使用 0dp 来启用权重 -->
        android:layout_height="wrap_content"
        android:text="Button 2"
        app:layout_constraintStart_toEndOf="@id/button1"
        app:layout_constraintEnd_toStartOf="@id/button3"
        app:layout_constraintHorizontal_weight="1" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp" <!-- 使用 0dp 来启用权重 -->
        android:layout_height="wrap_content"
        android:text="Button 3"
        app:layout_constraintStart_toEndOf="@id/button2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_weight="2" />

</androidx.constraintlayout.widget.ConstraintLayout>

5、layout_constrainedWidth

可以参考学习:
app:layout_constrainedWidth=“true” 属性,是按约束去限制宽度,如下图:
在这里插入图片描述
app:layout_constrainedWidth=“false”, 如下图:
在这里插入图片描述

举例:

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:background="#fafa"
        android:singleLine="true"
        android:text="Hello, World!"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:background="#deedfd"
        android:singleLine="true"
        android:text="textviewtextviewtexiview"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toEndOf="@id/textView1"
        app:layout_constraintStart_toStartOf="@id/textView1"
        app:layout_constraintTop_toBottomOf="@id/textView1" />
</androidx.constraintlayout.widget.ConstraintLayout>

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Windows双网卡冲突导致网页加载过慢的解决方法 (修改跃点无效 远程桌面连接)

【本文发布于https://blog.csdn.net/Stack_/article/details/145494160&#xff0c;未经许可不得转载&#xff0c;转载须注明出处】 办公室内&#xff0c;我的笔记本和台式机都连接WIFI进行上网&#xff0c;网段是192.168.0.x&#xff0c;网关192.168.0.101 现在要通过Windows自…

轻量级服务器http-server

安装 sudo npm install http-server -g 运行 1. 直接去到要跑起来的目录&#xff0c;在终端输入 cd xxxx文件夹http-server //只输入http-server的话&#xff0c;更新了代码后&#xff0c;页面不会同步更新http-server -c-1 //同步更新页面http-server -a 127.0.0.1 -p 808…

代码随想录算法【Day38】

Day38 322. 零钱兑换 思路 完全背包 代码 class Solution { public:int coinChange(vector<int>& coins, int amount) {vector<int> dp(amount 1, INT_MAX);dp[0] 0;for (int i 0; i < coins.size(); i) { // 遍历物品for (int j coins[i]; j <…

python+opencv+open3d实现鼠标手画多边形裁剪分割点云操作

👑主页:吾名招财 👓简介:工科学硕,研究方向机器视觉,爱好较广泛… ​💫签名:面朝大海,春暖花开! python+opencv+open3d实现鼠标手画多边形裁剪分割点云操作 引言使用效果:代码pcd_roi_crop.py:引言 当我们想对一个不规则物体的图像或者点云裁剪时,直接手动输入…

STM32的HAL库开发---通用定时器(TIMER)---定时器脉冲计数

一、脉冲计数实验原理 1、 外部时钟模式1&#xff1a;核心为蓝色部分的时基单元&#xff0c;时基单元的时钟源可以来自四种&#xff0c;分别是内部时钟PCLK、外部时钟模式1&#xff0c;外部时钟模式2、内部定时器触发&#xff08;级联&#xff09;。而脉冲计数就是使用外部时钟…

Redis05 - 性能调优和缓存问题

Redis性能调优和缓存问题 文章目录 Redis性能调优和缓存问题一&#xff1a;链路追踪判断是不是redis出了问题二&#xff1a;redis变慢原因1&#xff1a;使用复杂度过高的命令(*)1.1&#xff1a;查看redis慢日志1.2&#xff1a;延迟变大原因分析1.3&#xff1a;解决方案 2&#…

漫步 C++ 之途,领略引用的独特风姿

在C中&#xff0c;引用&#xff08;Reference&#xff09;是一种非常有用的特性&#xff0c;它允许为一个变量创建一个别名&#xff08;Alias&#xff09;。引用在很多情况下可以替代指针&#xff0c;但使用起来更加方便和安全。以下是对C引用的详细介绍&#xff0c;包括其定义…

Spring Boot Web 入门

目录 Spring Boot Web 是 Spring Boot 框架的一个重要模块&#xff0c;它简化了基于 Spring 的 Web 应用程序的开发过程。以下是一个 Spring Boot Web 项目的入门指南&#xff0c;涵盖了项目创建、代码编写、运行等关键步骤。 1. 项目创建 使用 Spring Initializr 使用 IDE …

Java 多线程、线程同步、线程池

一. 线程 1. 线程&#xff1a;线程(Thread)是一个程序内部的一条执行流程。 2. 程序中如果只有一条执行流程&#xff0c;那这个程序就是单线程的程序。 二. 多线程 多线程是指从硬件上实现多条执行流程的技术(多条线程由CPU负责调度) Javas是通过java.lang.Thread类的对象来代…

20.[前端开发]Day20-王者荣耀项目实战(三)

01_(掌握)王者荣耀-main-赛事新闻列表实现 <!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"UTF-8"><meta http-equiv"X-UA-Compatible" content"IEedge"><meta name"viewport" …

【Langchain学习笔记(一)】Langchain介绍

Langchain介绍 Langchain介绍前言1、Langchain 是什么2、为什么要用 Langchain3、Langchain 的核心4、Langchain 的底层原理5、Langchain 的应用场景 Langchain介绍 前言 想象一下&#xff0c;如果你能让聊天机器人不仅仅回答通用问题&#xff0c;还能从你自己的数据库或文件…

IDEA2024版本创建Sping项目无法选择Java 8

目录 一、背景二、解决方式&#xff08;替换创建项目的源地址&#xff09; 一、背景 IDEA2024创建一个springboot的项目&#xff0c;本地安装的是1.8&#xff0c;但是在使用Spring Initializr创建项目时&#xff0c;发现版本只有17、21、23。 二、解决方式&#xff08;替换创…

C++11(四)

目录 包装器 function包装器 bind绑定 更改实参传递的顺序和实参传递的个数 线程库 本期我们将继续进行C11新特性的学习。 包装器 function包装器 function包装器&#xff0c;我们也称之为适配器&#xff0c;本质上就是一个类模板&#xff0c;为什么要引入function包…

MySQL 数据库编程-C++

目录 1 数据库基本知识 1.1 MYSQL常见命令 1.2 SQL注入 1.3 ORM框架 1 数据库基本知识 MySQL 为关系型数据库(Relational Database Management System), 这种所谓的"关系型"可以理解为"表格"的概念, 一个关系型数据库由一个或数个表格组成&#xff1a…

【算法篇】贪心算法

目录 贪心算法 贪心算法实际应用 一&#xff0c;零钱找回问题 二&#xff0c;活动选择问题 三&#xff0c;分数背包问题 将数组和减半的最小操作次数 最大数 贪心算法 贪心算法&#xff0c;是一种在每一步选择中都采取当前状态下的最优策略&#xff0c;期望得到全局最优…

5 计算机网络

5 计算机网络 5.1 OSI/RM七层模型 5.2 TCP/IP协议簇 5.2.1:常见协议基础 一、 TCP是可靠的&#xff0c;效率低的&#xff1b; 1.HTTP协议端口默认80&#xff0c;HTTPSSL之后成为HTTPS协议默认端口443。 2.对于0~1023一般是默认的公共端口不需要注册&#xff0c;1024以后的则需…

动态规划LeetCode-1035.不相交的线

在两条独立的水平线上按给定的顺序写下 nums1 和 nums2 中的整数。 现在&#xff0c;可以绘制一些连接两个数字 nums1[i] 和 nums2[j] 的直线&#xff0c;这些直线需要同时满足&#xff1a; nums1[i] nums2[j]且绘制的直线不与任何其他连线&#xff08;非水平线&#xff09;相…

禅道社区版项目管理软件部署(记录篇)

系统要求&#xff08;这里推荐使用docker容器化方式&#xff09;安装前的准备Docker快速安装最后通过查看地址验证是否部署成功开始界面化安装配置 禅道&#xff08;ZenTao&#xff09;是一款国产开源的项目管理软件&#xff0c;专注于敏捷开发流程&#xff0c;支持 Scrum 和 K…

数据结构-基础

1、概念&#xff1a; 程序 数据结构 算法 2、程序的好坏 可读性&#xff0c;稳定性&#xff0c;扩展性&#xff0c;时间复杂度&#xff0c;空间复杂度。 3、数据结构 是指存储、组织数据的方式&#xff0c;以便高效地进行访问和修改。通过选择适当的数据结构&#xff0c; 能…

从零开始:OpenCV 图像处理快速入门教程

文章大纲 第1章 OpenCV 概述 1.1 OpenCV的模块与功能  1.2 OpenCV的发展 1.3 OpenCV的应用 第2章 基本数据类型 2.1 cv::Vec类 2.2 cv&#xff1a;&#xff1a;Point类 2.3 cv&#xff1a;&#xff1a;Rng类 2.4 cv&#xff1a;&#xff1a;Size类 2.5 cv&#xff1a;&…