鸿蒙Harmony-线性布局(Row/Column)详解

人生的下半场,做个简单的人,少与人纠缠,多看大自然,在路上见世界,在途中寻自己。往后余生唯愿开心健康,至于其他,随缘就好! 

目录

一,定义

二,基本概念

三,布局子元素在排列方向上的间距

四,布局子元素在交叉轴上的对齐方式

4.1 Column容器内子元素在水平方向上的排列

 4.1.1 HorizontalAlign.Start

 4.1.2 HorizontalAlign.Center

4.1.3 HorizontalAlign.End

4.2 Row容器内子元素在垂直方向上的排列

4.2.1 VerticalAlign.Top

4.2.2  VerticalAlign.Center

4.2.3 VerticalAlign.Bottom

五,布局子元素在主轴上的排列方式

5.1 Column容器内子元素在垂直方向上的排列

5.1.1 FlexAlign.Start

5.1.2 FlexAlign.Center

5.1.3 FlexAlign.End

5.1.4 FlexAlign.SpaceBetween

5.1.5 FlexAlign.SpaceAround

5.1.6 FlexAlign.SpaceEvenly

5.2 Row容器内子元素在水平方向上的排列

5.2.1 FlexAlign.Start

5.2.2 FlexAlign.Center

5.2.3 FlexAlign.End

5.2.4 FlexAlign.SpaceBetween

5.2.5 FlexAlign.SpaceAround

5.2.6 FlexAlign.SpaceEvenly

六,自适应拉伸

七, 自适应缩放

八,自适应延伸

8.1 在list中添加滚动条

 8.2 水平方向布局中使用Scroll组件

一,定义

鸿蒙采用了声明式的UI,它的线性布局是Row/Column,类似于Android中的LinearLayout。

线性布局是其他布局的基础,其子元素在线性方向上(水平方向和垂直方向)依次排列。线性布局的排列方向由所选容器组件决定,Column容器内子元素按照垂直方向排列,Row容器内子元素按照水平方向排列。

二,基本概念

布局容器:具有布局能力的容器组件,相当于Android中的viewgroup,可以承载其他元素作为其子元素,布局容器会对其子元素进行尺寸计算和布局排列。

布局子元素:布局容器内部的元素。

主轴:线性布局容器在布局方向上的轴线,子元素默认沿主轴排列。Row容器主轴为水平方向,Column容器主轴为垂直方向。

交叉轴:垂直于主轴方向的轴线。Row容器交叉轴为垂直方向,Column容器交叉轴为水平方向。

间距:布局子元素的间距。

三,布局子元素在排列方向上的间距

在布局容器内,可以通过space属性设置排列方向上子元素的间距,使各子元素在排列方向上有等间距效果。

Column:

@Entry()
@Component
struct Index {build() {Column({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}}
}

效果:

Row:

@Entry()
@Component
struct RowTest {build() {Row({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}}
}

运行效果

四,布局子元素在交叉轴上的对齐方式

在布局容器内,可以通过alignItems属性设置子元素在交叉轴(排列方向的垂直方向)上的对齐方式。且在各类尺寸屏幕中,表现一致。其中,交叉轴为垂直方向时,取值为VerticalAlign类型,水平方向取值为HorizontalAlign

alignSelf属性用于控制单个子元素在容器交叉轴上的对齐方式,其优先级高于alignItems属性,如果设置了alignSelf属性,则在单个子元素上会覆盖alignItems属性。

4.1 Column容器内子元素在水平方向上的排列

注意:必须指定Column的宽度,否则无效

 4.1.1 HorizontalAlign.Start

@Entry()
@Component
struct Index {build() {Column({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}.width("100%").alignItems(HorizontalAlign.Start)}
}

运行效果:

 4.1.2 HorizontalAlign.Center

@Entry()
@Component
struct Index {build() {Column({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}.width("100%").alignItems(HorizontalAlign.Center)}
}

运行效果

4.1.3 HorizontalAlign.End

@Entry()
@Component
struct Index {build() {Column({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}.width("100%").alignItems(HorizontalAlign.End)}
}

4.2 Row容器内子元素在垂直方向上的排列

注意:高度必须指定,否则无效

4.2.1 VerticalAlign.Top

@Entry()
@Component
struct RowTest {build() {Row({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}.height('100%').alignItems(VerticalAlign.Top)}
}

4.2.2  VerticalAlign.Center

@Entry()
@Component
struct RowTest {build() {Row({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}.height('100%').alignItems(VerticalAlign.Center)}
}

 

4.2.3 VerticalAlign.Bottom

@Entry()
@Component
struct RowTest {build() {Row({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}.height('100%').alignItems(VerticalAlign.Bottom)}
}

五,布局子元素在主轴上的排列方式

在布局容器内,可以通过justifyContent属性设置子元素在容器主轴上的排列方式。可以从主轴起始位置开始排布,也可以从主轴结束位置开始排布,或者均匀分割主轴的空间。

5.1 Column容器内子元素在垂直方向上的排列

5.1.1 FlexAlign.Start

@Entry()
@Component
struct Index {build() {Column({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}.width("100%").height("100%").justifyContent(FlexAlign.Start)}
}

5.1.2 FlexAlign.Center

@Entry()
@Component
struct Index {build() {Column({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}.width("100%").height("100%").justifyContent(FlexAlign.Center)}
}

5.1.3 FlexAlign.End

@Entry()
@Component
struct Index {build() {Column({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}.width("100%").height("100%").justifyContent(FlexAlign.End)}
}

5.1.4 FlexAlign.SpaceBetween

垂直方向均匀分配元素,相邻元素之间距离相同。第一个元素与行首对齐,最后一个元素与行尾对齐。

@Entry()
@Component
struct Index {build() {Column({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}.width("100%").height("100%").justifyContent(FlexAlign.SpaceBetween)}
}

5.1.5 FlexAlign.SpaceAround

垂直方向均匀分配元素,相邻元素之间距离相同。第一个元素到行首的距离和最后一个元素到行尾的距离是相邻元素之间距离的一半。

@Entry()
@Component
struct Index {build() {Column({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}.width("100%").height("100%").justifyContent(FlexAlign.SpaceAround)}
}

5.1.6 FlexAlign.SpaceEvenly

垂直方向均匀分配元素,相邻元素之间的距离、第一个元素与行首的间距、最后一个元素到行尾的间距都完全一样。

@Entry()
@Component
struct Index {build() {Column({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}.width("100%").height("100%").justifyContent(FlexAlign.SpaceEvenly)}
}

5.2 Row容器内子元素在水平方向上的排列

5.2.1 FlexAlign.Start

@Entry()
@Component
struct RowTest {build() {Row({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}.height('100%').width('100%').justifyContent(FlexAlign.Start)}
}

5.2.2 FlexAlign.Center

@Entry()
@Component
struct RowTest {build() {Row({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}.height('100%').width('100%').justifyContent(FlexAlign.Center)}
}

5.2.3 FlexAlign.End

@Entry()
@Component
struct RowTest {build() {Row({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}.height('100%').width('100%').justifyContent(FlexAlign.End)}
}

5.2.4 FlexAlign.SpaceBetween

水平方向均匀分配元素,相邻元素之间距离相同。第一个元素与行首对齐,最后一个元素与行尾对齐。

@Entry()
@Component
struct RowTest {build() {Row({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}.height('100%').width('100%').justifyContent(FlexAlign.SpaceBetween)}
}

5.2.5 FlexAlign.SpaceAround

水平方向均匀分配元素,相邻元素之间距离相同。第一个元素到行首的距离和最后一个元素到行尾的距离是相邻元素之间距离的一半。

@Entry()
@Component
struct RowTest {build() {Row({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}.height('100%').width('100%').justifyContent(FlexAlign.SpaceAround)}
}

5.2.6 FlexAlign.SpaceEvenly

@Entry()
@Component
struct RowTest {build() {Row({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}.height('100%').width('100%').justifyContent(FlexAlign.SpaceEvenly)}
}

六,自适应拉伸

在线性布局下,常用空白填充组件Blank,在容器主轴方向自动填充空白空间,达到自适应拉伸效果。Row和Column作为容器,只需要添加宽高为百分比,当屏幕宽高发生变化时,会产生自适应效果。

@Entry()
@Component
struct RowTest {build() {Row({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold)Blank()Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold)}.height('100%').width('100%')}
}

七, 自适应缩放

自适应缩放是指子元素随容器尺寸的变化而按照预设的比例自动调整尺寸,适应各种不同大小的设备。在线性布局中,可以使用以下两种方法实现自适应缩放。

7.1 父容器尺寸确定时,使用layoutWeight属性设置子元素和兄弟元素在主轴上的权重,忽略元素本身尺寸设置,使它们在任意尺寸的设备下自适应占满剩余空间。

@Entry()
@Component
struct Index {build() {Column({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold).backgroundColor("#00f").layoutWeight(1)Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold).backgroundColor("#0ff").layoutWeight(2)Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold).backgroundColor("#35f").layoutWeight(3)}.width("100%").height("100%")}
}

7.2 父容器尺寸确定时,使用百分比设置子元素和兄弟元素的高度,使他们在任意尺寸的设备下保持固定的自适应占比。

@Entry()
@Component
struct Index {build() {Column({space:20}){Text("袁震1").fontSize(50).fontWeight(FontWeight.Bold).backgroundColor("#00f").height("20%")Text("袁震2").fontSize(50).fontWeight(FontWeight.Bold).backgroundColor("#0ff").height("30%")Text("袁震3").fontSize(50).fontWeight(FontWeight.Bold).backgroundColor("#35f").height("50%")}.width("100%").height("100%")}
}

八,自适应延伸

自适应延伸是指在不同尺寸设备下,当页面的内容超出屏幕大小而无法完全显示时,可以通过滚动条进行拖动展示。这种方法适用于线性布局中内容无法一屏展示的场景。通常有以下两种实现方式。

8.1 在list中添加滚动条

当List子项过多一屏放不下时,可以将每一项子元素放置在不同的组件中,通过滚动条进行拖动展示。可以通过scrollBar属性设置滚动条的常驻状态,edgeEffect属性设置拖动到内容最末端的回弹效果。

@Entry
@Component
struct ScrollExample {scroller: Scroller = new Scroller();private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];build() {Scroll(this.scroller) {Column() {ForEach(this.arr, (item?:number|undefined) => {if(item){Text(item.toString()).width('90%').height(150).backgroundColor(0xFFFFFF).borderRadius(15).fontSize(16).textAlign(TextAlign.Center).margin({ top: 10 })}}, (item:number) => item.toString())}.width('100%')}.backgroundColor(0xDCDCDC).scrollable(ScrollDirection.Vertical) // 滚动方向为垂直方向.scrollBar(BarState.On) // 滚动条常驻显示.scrollBarColor(Color.Gray) // 滚动条颜色.scrollBarWidth(10) // 滚动条宽度.edgeEffect(EdgeEffect.Spring) // 滚动到边沿后回弹}
}

 8.2 水平方向布局中使用Scroll组件

@Entry
@Component
struct ScrollExample {scroller: Scroller = new Scroller();private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];build() {Scroll(this.scroller) {Row() {ForEach(this.arr, (item?:number|undefined) => {if(item){Text(item.toString()).height('90%').width(150).backgroundColor(0xFFFFFF).borderRadius(15).fontSize(16).textAlign(TextAlign.Center).margin({ left: 10 })}})}.height('100%')}.backgroundColor(0xDCDCDC).scrollable(ScrollDirection.Horizontal) // 滚动方向为水平方向.scrollBar(BarState.On) // 滚动条常驻显示.scrollBarColor(Color.Gray) // 滚动条颜色.scrollBarWidth(10) // 滚动条宽度.edgeEffect(EdgeEffect.Spring) // 滚动到边沿后回弹}
}

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

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

相关文章

如何使用“通义听悟”提高工作和学习效率

如何使用通义听悟提高工作和学习效率 通义听悟是一款利用人工智能技术,自动为音频和视频内容提供转写、翻译、总结、检索等功能的在线工具。它可以在会议、学习、访谈、培训等场景下,帮助您记录、阅读、整理、复习音视频信息,成为您的工作和…

FPGA 移位运算与乘法

题目: 已知d为一个8位数,请在每个时钟周期分别输出该数乘1/3/7/8,并输出一个信号通知此时刻输入的d有效(d给出的信号的上升沿表示写入有效) 由题意可知: 复位信号高有效,低复位;在inpu_grant上升…

WorkPlus卓越的即时通讯工具,助力企业提升工作效率

在当今快节奏的商业环境中,高效沟通和协作是企业成功的关键。而即时通讯作为实现高效沟通的利器,成为了现代企业不可或缺的一部分。作为一款领先的即时通讯工具,WorkPlus以其卓越的性能和独特的功能,助力企业打造高效沟通和协作的…

docker应用:搭建uptime-kuma监控站点

简介:Uptime Kuma是一个易于使用的自托管监控工具,它的界面干净简洁,部署和使用都非常方便。 历史攻略: docker:可视化工具portainer docker-compose:搭建自动化运维平台Spug 开源地址: ht…

【NI 国产替代】PXIe‑6378,16路AI(16位,3.5 MS/s/ch),4路AO,48路DIO,PXI多功能I/O模块

PXIe,16路AI(16位,3.5 MS/s/ch),4路AO,48路DIO,PXI多功能I/O模块 PXIe‑6378是一款同步采样的多功能DAQ设备。 该模块提供了模拟 I/O、数字I/O、四个32位计数器和模拟和数字触发。 板载NI‑STC3…

class_3:lambda表达式

1、lambda表达式是c11引入的一种匿名函数的方式&#xff0c;它允许你在需要函数的地方内联的定义函数&#xff0c;而无需单独命名函数&#xff1b; #include <iostream>using namespace std;bool compare(int a,int b) {return a > b; }int getMax(int a,int b,bool (…

算法通关村第十六关—滑动窗口与堆结合(黄金)

滑动窗口与堆结合 堆与滑动窗口问题的结合 LeetCode239给你一个整数数组nums,有一个大小为k的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的k个数字。滑动窗口每次只向右移动一位&#xff0c;返回滑动窗口中的最大值。  对于最大值、K个最大这种场…

RibbonGroup添加QCheckBox

RibbonGroup添加 QCheckBox&#xff1a; QCheckBox* pCheck new QCheckBox(tr("Check")); pCheck->setToolTip(tr("Check")); groupClipboard->addWidget(pCheck); connect(pCheck, SIGNAL(stateChanged(int)), this, SLOT(checkClick(int))); …

[学习笔记]刘知远团队大模型技术与交叉应用-汇总

参考资料&#xff1a; 视频&#xff1a;【清华NLP】刘知远团队大模型公开课全网首发&#xff5c;带你从入门到实战 课程计划 L1-NLP&Big Model Basics [学习笔记]刘知远团队大模型技术与交叉应用L1-NLP&Big Model Basics L2-Neural Network Basics [学习笔记]刘知…

【Java JVM】栈帧

执行引擎是 Java 虚拟机核心的组成部分之一。 在《Java虚拟机规范》中制定了 Java 虚拟机字节码执行引擎的概念模型, 这个概念模型成为各大发行商的 Java 虚拟机执行引擎的统一外观 (Facade)。 不同的虚拟机的实现中, 通常会有 解释执行 (通过解释器执行)编译执行 (通过即时编…

Github Copilot最全的安装与使用教程:一款非常好用的AI编程工具

Github Copilot最全的安装与使用教程 第一章 安装1.安装 GitHub Copilot2.获取资格第二章 使用1.产生建议1.1 键入你想要完成的操作的注释1.2 CtrlI 2. 接受建议3.查看下一个建议3.接受部分建议4.在新选项卡接受建议5.完成多项功能6.聊天 GitHub Copilot 供经过验证的学生、教师…

C语言经典算法之直接排序算法

目录 前言 一、代码实现 二、时空复杂度 时间复杂度&#xff1a; 空间复杂度&#xff1a; 前言 建议&#xff1a;1.学习算法最重要的是理解算法的每一步&#xff0c;而不是记住算法。 2.建议读者学习算法的时候&#xff0c;自己手动一步一步地运行算法。 tips:希尔排序算…

Flask架构--路由和蓝图

学习视频&#xff1a;第二章&#xff1a;路由和蓝图 1 Flask查询路由的方式_哔哩哔哩_bilibili 参考&#xff1a;Flask框架之路由与蓝图的使用_flask 路由和蓝图-CSDN博客 1.路由的概念&#xff1a; 用于将http请求与特定的python函数相匹配。定义路由后&#xff0c;flask程…

模型索引:QModelIndex

一、为什么要使用模型索引&#xff1f; 从名字可以看出&#xff0c;他是模型的索引&#xff0c;只要对模型实体&#xff08;各种xxxModel的实体&#xff09;施加这个索引&#xff0c;model就会返回数据集中对应的值&#xff0c;或者通过这个索引修改对应数据集中的值。 类比数…

Java内容

目录 1.命名规范 1.命名规范 2.变量

懒得玩游戏--帮我做数独

目录 简介自动解数独思路核心思路输入解析打印 完整代码 简介 最近玩上了一款类似于数独的微信小程序游戏&#xff0c;名字叫数独趣味闯关&#xff0c;过了数独的关卡之后会给拼图&#xff0c;玩了几关之后摸清套路了就有点累了&#xff0c;但是还想集齐拼图&#xff0c;所以就…

【AUTOSAR】--01 AUTOSAR网络管理基础

AUTOSAR网络管理做了几个项目了&#xff0c;但发现还是有些理解不够深入的地方&#xff0c;最近趁着有个新项目也要做AUTOSAR网络管理&#xff0c;再从头梳理一下AUTOSAR网络管理&#xff0c;预计用2-3篇文章&#xff0c;把AUTOSAR网络重新梳理完成。 这是第一篇&#xff0c;主…

Element-Puls Form表单内嵌套el-table表格,根据表格复选框多选或单选动态设置行的验证规则

需求 根据 Table 表格内的复选框来控制当前选中行是否添加必填校验规则 效果图 实现思想 我们需要设置一个 flag 来标识已勾选的行&#xff0c;el-table渲染数据结构是数组对象形式&#xff0c;我们可以在每个对象中手动加如一个标识&#xff0c;例如默认&#xff1a;selected …

Windows环境 elasticsearch 及可视化界面 安装

安装 elastic 的官网 elastic.co/downloads/elasticsearch 当你解压好了归档文件之后&#xff0c;Elasticsearch 已经准备好运行了。按照下面的操作&#xff0c;在前台(foregroud)启动 Elasticsearch&#xff1a; cd elasticsearch-<version> ./bin/elasticsearch 如…

大型语言模型综述/总结 LLM A Survey of Large Language Models

A Survey of Large Language Model AbstractINTRODUCTIONOVERVIEW背景LLM的新兴能力LLM的关键技术GPT 系列模型的技术演进 大语言模型资源公开可用的模型检查点或 API常用语料库代码库资源 预训练数据收集架构 论文标题&#xff1a;A Survey of Large Language Model 论文地址&…