ConstraintLayout布局扩展

news/2024/4/26 7:24:52/文章来源:https://blog.csdn.net/ZyClient/article/details/128099440

相信大家对ConstraintLayout(约束布局)不陌生,这是google推出的一个强大控件,之所以强大其实主要归纳有两点:减少布局层次,能够轻松实现复杂布局。当然在我们实际使用过程中,是否真的减少了布局层次?其实这个问题从我开始用这个布局产生了疑问,看了下自己写的布局还是一层包一层。如果你也有这个问题,那么接下来文章中的内容能够帮助你解决.

1.约束布局基本使用:

 相信大家对于基本使用应该都没什么问题,这里就不做过多阐述,跟四大控件里面的相对布局思路类似。

2.约束布局辅助控件:

辅助控件就是今天的重点内容:今天重点说下GuideLine,Layer,Flow,group这四个辅助控件。我先贴下我demo布局

<?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:id="@+id/cons"android:layout_width="match_parent"android:layout_height="match_parent"><androidx.constraintlayout.helper.widget.Layerandroid:id="@+id/layer"android:layout_width="0dp"android:layout_height="0dp"android:background="#D16D6D"app:constraint_referenced_ids="textview1,textview2,src"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/textview1"android:layout_width="0dp"android:layout_height="wrap_content"android:padding="10dp"android:text="这是一个constrainlayout自带的流式布局Flow"android:textColor="@color/white"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toLeftOf="@id/guideline"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/textview2"android:layout_width="0dp"android:layout_height="wrap_content"android:padding="10dp"android:text="这是一个constraintlayout自带的Layer层布局"android:textColor="@color/white"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toLeftOf="@id/guideline"app:layout_constraintTop_toBottomOf="@id/textview1" /><androidx.constraintlayout.widget.Guidelineandroid:id="@+id/guideline"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"app:layout_constraintGuide_percent="0.8" /><ImageViewandroid:id="@+id/src"android:layout_width="0dp"android:layout_height="wrap_content"android:padding="10dp"android:src="@mipmap/ic_launcher"app:layout_constraintBottom_toBottomOf="@id/textview2"app:layout_constraintLeft_toRightOf="@id/guideline"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="@id/textview1" /><androidx.constraintlayout.helper.widget.Flowandroid:id="@+id/demo_flow"android:layout_width="0dp"android:layout_height="wrap_content"app:flow_firstHorizontalBias="0"app:flow_firstHorizontalStyle="packed"app:flow_horizontalBias="0"app:flow_horizontalGap="10dp"app:flow_horizontalStyle="spread_inside"app:flow_lastHorizontalBias="0"app:flow_lastHorizontalStyle="packed"app:flow_verticalGap="8dp"app:flow_wrapMode="chain"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toBottomOf="@id/textview2" /></androidx.constraintlayout.widget.ConstraintLayout>

2.1 GuideLine辅助线

首先看布局中的guideLine控件:这个控件是不显示在界面上的,也不占用任何空间,主要用于辅助线,它分水平和垂直辅助线,上面布局就是垂直辅助线,我们看看预览效果

可以看到预览效果中在屏幕0.8宽度上有一条垂直辅助线,他可以作为左右两边控件的约束条件.因此guideline可以简单理解为:不显示,不占空间的一条辅助约束其他控件的线。

2.2 Layer控件

说到这个控件我就不得不提一下:在不知道这个辅助控件之前,我的xml布局还是一层套一层,为什么呐?因为我要给某几个控件设置一个背景色。相当于要管理几个子view,果断只有选择在外面包裹一层cons。我也多次在网上找了下资料是在无从下手,group也不行。就在最近在研究flow时突然看到这个控件,omg突然发现新大陆。layer就是层布局,他的作用感觉可以理解为帧布局。

看下xml中这段布局

<androidx.constraintlayout.helper.widget.Layerandroid:id="@+id/layer"android:layout_width="0dp"android:layout_height="0dp"android:background="#D16D6D"app:constraint_referenced_ids="textview1,textview2,src"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/textview1"android:layout_width="0dp"android:layout_height="wrap_content"android:padding="10dp"android:text="这是一个constrainlayout自带的流式布局Flow"android:textColor="@color/white"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toLeftOf="@id/guideline"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/textview2"android:layout_width="0dp"android:layout_height="wrap_content"android:padding="10dp"android:text="这是一个constraintlayout自带的Layer层布局"android:textColor="@color/white"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toLeftOf="@id/guideline"app:layout_constraintTop_toBottomOf="@id/textview1" /><androidx.constraintlayout.widget.Guidelineandroid:id="@+id/guideline"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"app:layout_constraintGuide_percent="0.8" /><ImageViewandroid:id="@+id/src"android:layout_width="0dp"android:layout_height="wrap_content"android:padding="10dp"android:src="@mipmap/ic_launcher"app:layout_constraintBottom_toBottomOf="@id/textview2"app:layout_constraintLeft_toRightOf="@id/guideline"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="@id/textview1" />

需要给管理的子控件id设置上使用app:constraint_referenced_ids,使用相当简单,另外建议把layer写在管理的子布局前面不然设置的背景可能无效,至于为什么你可以理解为帧布局,layer写在后面就会把子view覆盖无法正常显示.这样就解决了给多个子控件设置背景又套一层。不过这个控件我还没用到项目上,只是有写demo实例。

2.3 flow控件

这个控件就是我们说的流式布局,在以往我们现实这种效果需要自定义管理器,view或者第三方框架。目前google在cons中已经开源了flow控件。使用也相对简单:

<?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="wrap_content"><androidx.constraintlayout.helper.widget.Layerandroid:layout_width="match_parent"android:layout_height="match_parent"app:constraint_referenced_ids="tv_device_name,tv_online_state,tv_switch_state2"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/tv_device_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ellipsize="end"android:maxLines="1"android:padding="5dp"android:text="测试测试"android:textColor="@color/purple_200"android:textStyle="bold"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/tv_org_name"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginTop="16dp"android:text="研发部dadfad打发士大夫打发范德萨"android:textColor="@color/purple_700"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toLeftOf="@id/guideline"app:layout_constraintTop_toBottomOf="@id/tv_device_name" /><androidx.constraintlayout.widget.Guidelineandroid:id="@+id/guideline"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"app:layout_constraintGuide_percent="0.3" /><!--        //chain或者aligned,chain:链形式,依次挨着排,aligned会两端对齐--><!--        flow_firstHorizontalStyle首行的对齐方式,packed:靠最左侧挨着排,水平间隔:horizontalGap生--><!--        app:flow_horizontalBias="0" 全局水平bias,为0时,每行都贴左边,可解决中间行单独占一行时,不贴最左侧的问题--><!--        flow_horizontalGap  控件水平方向上的间隔--><androidx.constraintlayout.helper.widget.Flowandroid:id="@+id/flow"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginStart="5dp"android:layout_marginEnd="5dp"app:constraint_referenced_ids="tv_type,tv_online_state,tv_switch_state,tv_switch_state2,tv_switch_state3,tv_switch_state4,tv_switch_state5"app:flow_firstHorizontalBias="0"app:flow_firstHorizontalStyle="packed"app:flow_horizontalGap="10dp"app:flow_horizontalStyle="spread_inside"app:flow_lastHorizontalBias="0"app:flow_lastHorizontalStyle="packed"app:flow_verticalGap="8dp"app:flow_wrapMode="chain"app:layout_constraintLeft_toRightOf="@id/guideline"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="@id/tv_org_name" /><TextViewandroid:id="@+id/tv_type"android:layout_width="0dp"android:layout_height="wrap_content"android:text="自动采集看接口接gfgg"android:textColor="@color/black"app:layout_constraintWidth_default="wrap"/><TextViewandroid:id="@+id/tv_online_state"android:layout_width="0dp"android:layout_height="wrap_content"android:text="在线"android:textColor="@color/black"app:layout_constraintWidth_default="wrap"/><TextViewandroid:id="@+id/tv_switch_state"android:layout_width="0dp"android:layout_height="wrap_content"android:text="合位"android:textColor="@color/black"app:layout_constraintWidth_default="wrap" /><TextViewandroid:id="@+id/tv_switch_state2"android:layout_width="0dp"android:layout_height="wrap_content"android:text="的反馈进度"android:textColor="@color/black"app:layout_constraintWidth_default="wrap" /><TextViewandroid:id="@+id/tv_switch_state3"android:layout_width="0dp"android:layout_height="wrap_content"android:text="的反馈进"android:textColor="@color/black"app:layout_constraintWidth_default="wrap" /><TextViewandroid:id="@+id/tv_switch_state4"android:layout_width="0dp"android:layout_height="wrap_content"android:text="的反馈进度接口"android:textColor="@color/black"app:layout_constraintWidth_default="wrap" /><TextViewandroid:id="@+id/tv_switch_state5"android:layout_width="0dp"android:layout_height="wrap_content"android:text="的反馈进度接口"android:textColor="@color/black"app:layout_constraintWidth_default="wrap" />
</androidx.constraintlayout.widget.ConstraintLayout>

效果图

这个控件属性较多,理解起来不难,主要是应用场景:1.列表 2.历史搜索

列表里面比较简单直接在item里面对需要自动换行的控件设置id,类似上面的布局。

第二种历史搜索我们模拟一个list<String>的集合,控件进行动态生成(flow部分):

<androidx.constraintlayout.helper.widget.Flowandroid:id="@+id/demo_flow"android:layout_width="0dp"android:layout_height="wrap_content"app:flow_firstHorizontalBias="0"app:flow_firstHorizontalStyle="packed"app:flow_horizontalBias="0"app:flow_horizontalGap="10dp"app:flow_horizontalStyle="spread_inside"app:flow_lastHorizontalBias="0"app:flow_lastHorizontalStyle="packed"app:flow_verticalGap="8dp"app:flow_wrapMode="chain"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toBottomOf="@id/textview2" />

可以看到在flow布局中并没有设置ids,因此在我们页面中需要动态添加才能生效,下面给出java代码:

stringList.add("哈哈哈大打瞌睡");
stringList.add("阿萨第三方收费");
stringList.add("收到反馈的弗兰克");
stringList.add("世界杯加到快及点击");
stringList.add("大幅度发");
stringList.add("反反复复的付过付过滚滚滚");
stringList.add("哦单开反馈几点开奖辅助");
stringList.add("电饭锅热豆浆大富科技的");
stringList.add("如反馈及风科技风科技的”");
stringList.add("放入局大富科技风科技局非共和国和");
        int ids[] = new int[stringList.size()];int localis[] = new int[]{R.id.tvUserCode, R.id.tvUserName, R.id.tvTestDate, R.id.tvHumidity, R.id.tvPercent, R.id.tvCircle, R.id.tvTestSkin, R.id.tvHirstory, R.id.tva, R.id.tvb, R.id.tvc, R.id.tvd, R.id.tve, R.id.tvf};for (int index = 0; index < stringList.size(); index++) {TextView textView = new TextView(this);ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);layoutParams.matchConstraintDefaultWidth=ConstraintLayout.LayoutParams.WRAP_CONTENT;textView.setLayoutParams(layoutParams);textView.setId(localis[index]);
//            textView.setGravity(Gravity.START);
//            textView.setTextSize(16.0f);textView.setTextColor(getResources().getColor(R.color.black));textView.setText(stringList.get(index));cons.addView(textView);ids[index] = localis[index];textView.setOnClickListener(v -> {Log.e("您点击了", textView.getText().toString());Toast.makeText(MainActivity.this, "您点击了" + textView.getText(), Toast.LENGTH_SHORT).show();});}
flow.setReferencedIds(ids);

这里面重点就是setReferencedIds设置控件的id,需要传一个int[].这样我们就可以动态实现自动换行了,跟记录历史搜索功能类似,搜索内容进行自动换行。看下效果图

 

重点是中间部分,他实现了自动换行功能。因此在项目中我们需要自动换行时可以选择此控件实现,不需要接住第三方或者自定义。

2.4 group控件

这个辅助控件目前个人觉得功能比较单一就是分组管理,同样需要设置控件id对其进行统一管理,他与flow不同的是不能设置背景,一般是用于对其控件gone visiable的。这里就不单独贴出布局了。

总结:约束布局的其他辅助控件就不列出了,总之功能比较强大的,我们平时在项目中可能使用得不多,导致我们忽略了这个控件的优势,因此这些辅助控件还是值得大家学习,特别是layer能够统一设置控件背景。可以减少避免不必要的布局层次。

 

  

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

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

相关文章

FPGA实现图像对比度自动调整,提供2套工程源码和技术支持

目录1.算法原理介绍2.HLS算法实现3.工程1介绍&#xff1a;zynq7100实现4.工程2介绍&#xff1a;Kintex7实现5.上板调试验证6.福利&#xff1a;工程源码获取1.算法原理介绍 使用直方图均衡技术实现 使用直方图均衡技术将原始图像的灰度直方图从比较集中的某个灰度区间变成在全部…

ASCHIP_ISP Tool 工具 使用与更新

系列文章目录 ASCHIP-ISP Tool 版本1 2022 11 26ASCHIP-ISP Tool 版本1 使用说明 2022 11 26 软件介绍 对窗体进行初步配置&#xff0c;配置其大小与显示 一&#xff1a;软件介绍 ASCHIP_ISP Tool 工具是用于具有 ISP 功能型号类型的单片机进行 ISP 更新程序开发实验的配套上位…

[附源码]Python计算机毕业设计SSM隆庆祥企业服装销售管理系统(程序+LW)

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

【网络编程】第二章 网络套接字(socket+UDP协议程序)

&#x1f3c6;个人主页&#xff1a;企鹅不叫的博客 ​ &#x1f308;专栏 C语言初阶和进阶C项目Leetcode刷题初阶数据结构与算法C初阶和进阶《深入理解计算机操作系统》《高质量C/C编程》Linux ⭐️ 博主码云gitee链接&#xff1a;代码仓库地址 ⚡若有帮助可以【关注点赞收藏】…

Java-泛型实验

1.定义一个学生类Student&#xff0c;具有年龄age和姓名name两个属性&#xff0c;并通过实现Comparable接口提供比较规则&#xff08;返回两个学生的年龄差&#xff09;&#xff0c; 定义测试类Test&#xff0c;在测试类中定义测试方法Comparable getMax(Comparable c1, Compar…

Docker-JenKins安装及配置!

Jenkins官网&#xff1a;Jenkins 安装主机配置&#xff08;官方&#xff09;&#xff1a; 最低&#xff1a; 256 MB 内存 1 GB 的驱动器空间&#xff08;尽管如果将 Jenkins 作为 Docker 容器运行&#xff0c;则建议至少 10 GB&#xff09; 小团队推荐&#xff1a; 4 GB …

英文ppt怎么翻译成中文?教你几种ppt翻译方法

ppt文件受到很多人的喜欢是因为它展示起来直观清晰&#xff0c;无论是老师在课堂上使用ppt课件来教学&#xff0c;还是在工作汇报中用ppt文件展示设计的方案或取得的成果。但当我们需要把ppt文档里的文本内容里的外语翻译成中文的时候&#xff0c;我们应该怎么做呢&#xff1f;…

003. 电话号码的字母组合——回溯算法

1.题目链接&#xff1a; 17. 电话号码的字母组合 2.解题思路&#xff1a; 2.1.题目要求&#xff1a; 给定一个仅包含数字 2-9 的字符串 digits &#xff0c;返回所有它能表示的字母组合。 数字和字母的关系&#xff1a; 例子&#xff1a; 输入&#xff1a;"23" …

[Spring]第二篇:IOC控制反转

简单的说就是,创建对象的权利,或者是控制的位置,由JAVA代码转移到spring容器,由spring的容器控制对象的创建,就是控制反转. spring创建对象时,会读取配置文件,配置文件中主要配置接口和实现类的关系,每个接口对相应一个实现类,使用<bean>标签配置,<bean中的id可以随便…

学生选课系统

项目描述 通过项目背景的分析以及了解到现在学校面临的问题&#xff0c;特别需要一个选课管理系统保证学生信息以及各种课程成绩的准确性和实效性&#xff0c;通过利用计算机的高速计算和快速的统计分析&#xff0c;保证学生信息的最新记录。从教职工的角度老考虑&#xff0c;…

用VS软件开发“中国象棋“游戏<笔记摘录>

整体架构如上 1.很直观地去看这个中国象棋的界面,数一下它有多少行和多少列. 10行,9列:要注意这里数的是安放象棋的位置,有10行9列 这里我们首先想到的必然是二维数组,每一个行列交叉的点都设置成二维数组a[i][j]这样的格式,以此来确定棋盘上面每一个棋子的位置和走向. 我们…

详解 Spring Boot 项目中的配置文件

目录 1. Spring Boot 项目中配日文件的作用是什么 2. Spring Boot 配置文件的两种格式 3. properties 配置文件 3.1 properties 配置文件的基本语法 3.2 properties 配置文件的分类 3.3 如何读取配置文件 3.4 properties 配置文件的优缺点分析 4. yml 配置文件 4.1 yml …

BP神经网络PID从Simulink仿真到PLC控制实现(含博途PLC完整SCL源代码)

单神经元自适应PID控制博途PLC完整源代码,请参看下面的文章链接: 博途PLC单神经元自适应PID控制_RXXW_Dor的博客-CSDN博客_单神经元pid控制1、单神经元作为构成神经网络的基本单位,具有自学习和自适应能力,且结构简单易于计算,传统的PID具有结构简单、调整方便和参数整定…

【软件测试】8年资深测试说出来我们的心声......

目录&#xff1a;导读前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09;前言 执着于手动的功能测…

SSM毕设项目 - 基于SSM的毕业设计管理系统(含源码+论文)

文章目录1 项目简介2 实现效果2.1 界面展示3 设计方案3.1 概述3.2 系统流程3.2.1 系统开发流程3.3.2 教师登录流程3.3.3 系统操作流程3.3 系统结构设计4 项目获取1 项目简介 Hi&#xff0c;各位同学好呀&#xff0c;这里是M学姐&#xff01; 今天向大家分享一个今年(2022)最新…

【Android App】实战项目之仿抖音的短视频分享App(附源码和演示视频 超详细必看)

需要全部代码请点赞关注收藏后评论区留言私信~~~ 与传统的影视行业相比&#xff0c;诞生于移动互联网时代的短视频是个全新行业&#xff0c;它制作方便又容易传播&#xff0c;一出现就成为大街小巷的时髦潮流。 各行各业的人们均可通过短视频展示自己&#xff0c;短小精悍的视频…

社区系统项目复盘-6

文章目录什么是Elasticsearch&#xff1f;Spring是怎么整合Elasticsearch的&#xff1f;开发社区搜索功能Elasticsearch实现全文搜索功能什么是Elasticsearch&#xff1f; Elasticsearch简介 一个分布式的、Restful风格的搜索引擎支持对各种类型的数据的检索搜索速度快&#xf…

基于粒子群算法和遗传算法优化的高速列车横向悬挂

目录 前言 1.高速列车模型 2.优化算法优化模糊PID流程 3.普通PID、优化算法模糊PID仿真对比 3.1 模糊控制器设计 3.2 仿真结果 3.2.1粒子群优化PID 3.2.2粒子群优化模糊PID 3.2.3遗传算法优化模糊PID 4.总结 前言 高速列车&#xff0c;是指最高行驶速度在200km/h 及以…

小知识· Zigbee 简介

1. 介绍 ZigBee是一种近距离、低复杂度、低功耗、低速率、低成本的双向无线通讯技术 ZigBee建立在IEEE 802.15.4标准&#xff08;定义了PHY和MAC层&#xff09;之上&#xff0c;ZigBee联盟对其网络层和应用层进行了标准化 ZigBee协议栈可分为五层 - 物理层&#xff08;PHY&a…

多进程并发服务器

TCP三次握手建立连接错误处理模块&#xff1a;wrap.c,函数声明&#xff1a;wrap.h并发服务器模型&#xff08;多进程&#xff0c;多线程&#xff09; 转换大小写程序 服务端 #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #incl…