今天想看点干货?Constraintlayout等你好久啦!

news2024/9/23 3:27:32

大家好,我是小布丁。

从今天开始要开一个新坑啦,我会系统地把我学到的Android基础知识分享出来,我也想过这样是否有必要,毕竟分享的都是一些很基础的内容,但是很多人也和我一样正处于Android小白阶段,我能理解的东西小白应该也很好理解,所以这个专栏还是有意义的。文章哪里有问题还希望大家能够多多留言,共同进步!

什么是ConstraintLayout?

从 Android Studio 2.3起,创建的layout文件就已经是默认ConstraintLayout了,这是 Android 支持库中提供的一种新型、灵活且高效的布局。ConstraintLayout采用方向约束的方式对控件进行定位,水平和垂直方向都至少有一个约束才能确定控件的位置。

基本方向约束

实现一个控件的顶部和界面顶部对齐,左部和界面左部对齐:


<TextView
  android:layout_width="100dp" //设定文本框的宽
  android:layout_height="60dp" //设定文本框的高
  android:background="@drawable/tv_bg" //设定文本框背景
  android:gravity="center" //文字在文本框中居中显示
  android:text="A" //文本内容
  //重点在下面两行
  app:layout_constraintLeft_toLeftOf="parent" //文本框和父容器的左侧对齐
  app:layout_constraintTop_toTopOf="parent" //文本框和父容器的顶部对齐
/>

约束属性

app:layout_constraintTop_toTopOf=""           控件的顶部和谁的顶部对齐
app:layout_constraintBottom_toBottomOf=""     控件的底部和谁的底部对齐
app:layout_constraintLeft_toLeftOf=""         控件的左边和谁的左边对齐
app:layout_constraintRight_toRightOf=""       控件的右边和谁的右边对齐
app:layout_constraintStart_toStartOf=""       控件的开始位置和谁的开始位置对齐
app:layout_constraintEnd_toEndOf=""           控件的结束位置和谁的结束位置对齐
app:layout_constraintTop_toBottomOf=""        控件的顶部位置在谁的底部位置
app:layout_constraintStart_toEndOf=""         控件的开始位置在谁的结束为止

角度约束

实现一个控件在另一个控件的某个角度的位置。

app:layout_constraintCircle=""         //目标控件id
app:layout_constraintCircleAngle=""    //对于目标的角度(0-360)
app:layout_constraintCircleRadius=""   //到目标中心的距离

实现代码:

app:layout_constraintCircle="@id/pic1"
app:layout_constraintCircleAngle="45"
app:layout_constraintCircleRadius="50dp"

百分比偏移

实现百分比偏移的前提:

必须设置水平方向的两个约束(Left和Right),才能使用水平方向偏移属性app:layout_constraintHorizontal_bias=""(取值范围是0-1的小数)。必须设置垂直方向的两个约束(Top和Bottom),才能使用垂直方向偏移属性 app:layout_constraintVertical_bias="" (取值范围是0-1的小数)。

示例:


app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
//水平方向从左到右偏移的比例
app:layout_constraintHorizontal_bias="0.1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
//垂直方向从上到下偏移的比例
app:layout_constraintVertical_bias="0.1"

控件内边距、外边距

//外边距android:layout_margin="0dp"android:layout_marginLeft="0dp"android:layout_marginRight="0dp"android:layout_marginTop="0dp"android:layout_marginBottom="0dp"
//内边距android:layout_padding="0dp"android:layout_paddingLeft="0dp"android:layout_paddingRight="0dp"android:layout_paddingTop="0dp"android:layout_paddingBottom="0dp"

控件尺寸

在constraintlayout中有尺寸限制的属性,可以限制控件的最大最小尺寸,但是必须把控件对应的长/宽设为wrap_content。

android:minWidth=""   //设置view的最小宽度android:minHeight=""  //设置view的最小高度android:maxWidth=""   //设置view的最大宽度android:maxHeight=""  //设置view的最大高度

链(Chains)

一组通过双向连接链接在一起的小部件被视为一个链条。链条的行为由设置在链条第一个控件(即“链头”)上的属性控制:对于水平链条,链头是最左边的控件;对于垂直链条,链头是最上面的控件。

图片

链式样式当在链的第一个控件上设置属性layout_constraintHorizontal_chainStyle或layout_constraintVertical_chainStyle时,链的行为将根据指定的样式改变(默认是CHAIN_SPREAD)。

CHAIN_SPREAD:控件将被均匀分布(默认样式)。

CHAIN_SPREAD_INSIDE:类似于CHAIN_SPREAD,但链的端点不会被分散开来。

CHAIN_PACKED:链中的控件将被紧密打包在一起。然后,子控件的水平或垂直偏移属性将影响打包控件的定位。

图片

权重链

可以使用layout_constraintHorizontal_weight和layout_constraintVertical_weight进行设置链控件的权重。

<TextView
        android:id="@+id/A"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="@color/design_default_color_primary"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/B"
        //指定A的权重
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_chainStyle="packed" />
<TextView
        android:id="@+id/B"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="@color/design_default_color_primary_dark"
        app:layout_constraintLeft_toRightOf="@id/A"
        app:layout_constraintRight_toLeftOf="@id/C"
        //指定B的权重
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintTop_toTopOf="parent" />
<TextView
        android:id="@+id/C"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="@color/design_default_color_secondary"
        app:layout_constraintLeft_toRightOf="@id/B"
        app:layout_constraintRight_toRightOf="parent"
        //指定C的权重
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintTop_toTopOf="parent" />

Guideline(参考线)

实用工具类,代表ConstraintLayout中的Guideline辅助对象。辅助对象在设备上不显示(它们被标记为View.GONE),仅用于布局目的。它们只在ConstraintLayout内部工作。

android:orientation="horizontal|vertical"  辅助线的对齐方式app:layout_constraintGuide_percent="0-1"   距离父级宽度或高度的百分比(小数形式)app:layout_constraintGuide_begin=""        距离父级起始位置的距离(左侧或顶部)app:layout_constraintGuide_end=""          距离父级结束位置的距离(右侧或底部)

设计一个GuideLine,控件在GuideLine下方。


<androidx.constraintlayout.widget.Guideline
        android:id="@+id/Guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />

    <TextView
        android:id="@+id/A"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/design_default_color_secondary"
        android:gravity="center"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/Guideline" />

barrier

用一个示例来理解barrier:

有两个控件text1和text2,这两个控件纵向排列,长度不同,text3在这两个控件的右侧。如果设计text3的app:layout_constraintLeft_toRightOf text1或text2也可以,但是当不确定text1和text2谁的长度更长的时候就会出现问题。使用barrier可以处理这种情况,barrier的位置在text1和text2中最宽的控件的右侧,再让text3在barrier的右侧,问题解决。

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
        <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text2222222222222"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView1" />
        <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        //barrier在给定几个控件的后面
        app:barrierDirection="end"
        //取值是要给定的控件的id,barrier将会使用ids中最大的一个的宽/高作为自己的位置
        app:constraint_referenced_ids="textView2,textView1" />
        <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="text3"
        app:layout_constraintStart_toEndOf="@+id/barrier"
        app:layout_constraintTop_toTopOf="parent" />

图片

Group(组)

Group的作用就是可以对一组控件同时隐藏或显示。

<TextView
        android:id="@+id/A"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/design_default_color_primary"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
<TextView
        android:id="@+id/B"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/design_default_color_primary_dark"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/A"
        />
<androidx.constraintlayout.widget.Group
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        //A B这两个控件都由Group控制,当android:visibility="gone"时,这两个控件都不显示
        android:visibility="visible"        
        app:constraint_referenced_ids="A,B" />

Placeholder

Placeholder指的是占位符。在Placeholder中可使用setContent()设置另一个控件的id,使这个控件移动到占位符的位置。


<TextView
        android:id="@+id/textview"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/design_default_color_primary_variant"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

<androidx.constraintlayout.widget.Placeholder
        android:layout_width="50dp"
        android:layout_height="50dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

当我们设置app:content="@+id/A"或者调用setContent()时,控件A就会被移动到Placeholder中。

参考:《第一行代码》 

            官网

            https://www.jianshu.com/p/17ec9bd6ca8a

END

最后再自我介绍一下,我是小布丁,在一家互联网中厂做程序员,有一个公众号:码农小家园,会分享自己的生活和热爱,还有和互联网相关的趣事,欢迎大家来玩哦~

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

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

相关文章

RedHat9 | DNS剖析-配置转发DNS服务器

一、实验环境 1、转发DNS服务器 转发服务器&#xff08;Forwarding Server&#xff09;接受查询请求&#xff0c;但不直接提供DNS解析&#xff0c;而是将所有查询请求发送到另外一台DNS服务器&#xff0c;查询到结果后保存在本地缓存中。如果没有指定转发服务器&#xff0c;D…

PyCharm基本配置内容

如何更换 Python 解释器 输入一段代码点击运行后&#xff0c;画面下方有一个路径如图中框中所示&#xff1a; 上面的路径为虚拟路径&#xff0c;可以改为我们自己设置的路径 点击设置&#xff0c;选择settings 选择Project&#xff1a;y002———》Python Interpreter&#…

MySQL 重启之后无法写入数据了?

数据库交接后因 persist_only 级别的参数设置引发的故障分析。 作者&#xff1a;不吃芫荽&#xff0c;爱可生华东交付服务部 DBA 成员&#xff0c;主要负责 MySQL 故障处理及相关技术支持。 爱可生开源社区出品&#xff0c;原创内容未经授权不得随意使用&#xff0c;转载请联系…

解密Prompt系列15. LLM Agent之数据库应用设计:DIN C3 SQL-Palm BIRD

上一章我们主要讲搜索引擎和LLM的应用设计&#xff0c;这一章我们来唠唠大模型和DB数据库之间的交互方案。有很多数据平台已经接入&#xff0c;可以先去玩玩再来看下面的实现方案&#xff0c;推荐 [sql translate]&#xff1a;简单&#xff0c;文本到SQL&#xff0c;SQL到文本…

【PID算法详解】

PID算法 PID算法介绍用途pid数学表达式及其含义P算法D算法I算法 PID总结数学公式转换代码设计实际运用PID代码实现 PID算法介绍 PID控制器是一种广泛应用于工业控制系统的反馈控制器&#xff0c;它通过比例&#xff08;Proportional&#xff09;、积分&#xff08;Integral&am…

LeetCode199二叉树的右视图

题目描述 给定一个二叉树的 根节点 root&#xff0c;想象自己站在它的右侧&#xff0c;按照从顶部到底部的顺序&#xff0c;返回从右侧所能看到的节点值。 解析 这一题的关键其实就是找到怎么去得到当前是哪一层级&#xff0c;可以利用队列对二叉树进行层次遍历&#xff0c;但…

4款让人骄傲的国产软件,功能过于强大,却被误认为是外国佬研发

说到国产软件&#xff0c;许多人可能会有“流氓软件、弹屏广告多、隐藏消费套路”等负面印象。 这种偏见导致一些功能强大、用户友好的国产软件被误认为是外国人开发的。 1、格式工厂 格式工厂是一个很实用的国产格式转换工具&#xff0c;它完全免费且没有广告&#xff0c;不…

Dropzone 4 for Mac:一拖即达,文件处理更高效!

在繁忙的工作中&#xff0c;你是否曾因频繁切换应用程序和文件夹而烦恼&#xff1f;Dropzone 4 for Mac&#xff0c;这款强大的文件拖拽操作工具&#xff0c;将彻底改变你的工作方式&#xff01; 只需简单地将文件、文本或图片拖放到Dropzone图标上&#xff0c;即可快速执行各种…

一文搞定jdk8升级到jdk11

一、背景 为什么要升级JDK11 性能 JDK11的G1的GC性能高很多&#xff0c;对比JDK8无论是性能还是内存占比都有很大的提升&#xff0c;业内各项数据指标也都表明JDK11的G1在应对突发流量的下的效果惊人&#xff1b; 版本兼容 Spring Boot 2.7.x及以后的版本将不再支持Java 8作为…

Python开发 —— 对象type、object、class

1. "Python中一切皆为对象"的理解 在Python中&#xff0c;一切皆为对象的意思是指&#xff1a;无论是数字、字符串、函数、类、模块等任何数据类型&#xff0c;都可以被看做是一个对象。每个对象都具有自己的属性和方法&#xff0c;可以被操作和调用。 例如&#xff…

IP地址SSL证书应用场景以及如何申请?

一&#xff1a;IP地址SSL证书主要应用于以下几种场景&#xff1a; 1.API接口保护&#xff1a;许多云服务和企业内部系统使用IP地址直接作为服务的访问点&#xff0c;特别是在API接口的调用中。IP地址SSL证书可以为这些API接口提供必要的安全加密&#xff0c;确保数据在传输过程…

DreamPose: Fashion Image-to-Video Synthesis via Stable Diffusion

UW&UCB&Google&NVIDIA ICCV23https://github.com/johannakarras/DreamPose?tabreadme-ov-file 问题引入 输入参考图片 x 0 x_0 x0​和pose序列 { p 1 , ⋯ , p N } \{p_1,\cdots,p_N\} {p1​,⋯,pN​}&#xff0c;输出对应视频 { x 1 ′ , ⋯ , x N ′ } \{x_1,…

停车场变综合楼,结构分析助力低碳设计

PLAXIS 和 RAM 助力确定更有效的结构设计并大幅降低施工成本 总部和周边区域 桑坦德银行位于英国的新总部将现有的四个英国办事处合并到米尔顿凯恩斯的一个中心枢纽&#xff0c;位于伦敦以北 50 英里。 Unity Place 将作为桑坦德银行约 5,000 名员工的办公场所。该项目总投资 …

AIGC笔记--基于PEFT库使用LoRA

1--相关讲解 LORA: LOW-RANK ADAPTATION OF LARGE LANGUAGE MODELS LoRA 在 Stable Diffusion 中的三种应用&#xff1a;原理讲解与代码示例 PEFT-LoRA 2--基本原理 固定原始层&#xff0c;通过添加和训练两个低秩矩阵&#xff0c;达到微调模型的效果&#xff1b; 3--简单代…

web自动化-下拉框操作/键鼠操作/文件上传

在我们做UI自动化测试的时候&#xff0c;会有一些元素需要特殊操作&#xff0c;比如下拉框操作/键鼠操作/文件上传。 下拉框操作 在我们很多页面里有下拉框的选择&#xff0c;这种元素怎么定位呢&#xff1f;下拉框分为两种类型&#xff1a;我们分别针对这两种元素进行定位和…

答应我!养猫就一定要入手的七款好物!养猫真的会开心

养猫是一件让人愉悦的事情&#xff0c;猫咪的陪伴能让我们感到温暖和满足。然而&#xff0c;想要让猫咪健康快乐地成长&#xff0c;除了关心它们的饮食和健康&#xff0c;还需要为它们准备一些必要的生活用品。今天&#xff0c;我将为大家推荐几个养猫必备的好物&#xff0c;让…

黑马头条day6总结

1、wemedian错误 一开始没加EnableFeignClients(basePackages "com.heima.apis")导致获取ischeduleClient错误&#xff0c;找不到bean。 我看教程的代码中没有&#xff0c;【ComponentScan({"com.heima.apis","com.heima.wemedia"})】&#x…

11款必备IP地址管理软件,你都用过吗?

1、LightMesh IPAM 产品描述&#xff1a;LightMesh IPAM 是一款功能强大的工具&#xff0c;可简化和自动化互联网协议网络的管理。它提供可扩展性、子网规划器、即时云发现、IP 和网络管理以及 IP 规划和可视化&#xff0c;以帮助您优化效率、可见性和安全性。 特征&#xff1…

强化学习——学习笔记

一、什么是强化学习&#xff1f; 强化学习 (Reinforcement Learning, RL) 是一种通过与环境交互来学习决策策略的机器学习方法。它的核心思想是让智能体 (Agent) 在执行动作 (Action)、观察环境 (Environment) 反馈的状态 (State) 和奖励 (Reward) 的过程中&#xff0c;学习到…

C++音视频开发面试题集锦

老规矩&#xff0c;先上面试题目&#xff1a; 1、iOS 中系统 API 提供了哪些视频编码的方式&#xff1f;2、VideoToolbox 视频帧解码失败以后应该如何重试&#xff1f;3、如何使用 PSNR 对视频转码质量进行评估&#xff1f;4、什么是 VAO&#xff0c;什么是 VBO&#xff0c;它…