HarmonyOS鸿蒙开发常用4种布局详细说明

介绍一下鸿蒙开发常用4种布局

1、线性布局
2、层叠布局
3、网格布局
4、列表布局

​1. 线性布局(Column/Row)

线性布局(LinearLayout)是开发中最常用的布局,通过线性容器Row(行)和Column(列)构建,它是其他布局的基础,其子元素在线性方向上(水平或垂直)依次排列,基本形式如下:
Column(列)
子元素在排列方向上的间距,可以通过组件参数space参数进行控制

@Entry
@Component
struct Index {build() {Column({space:20}) {//一行Row() {}.width('80%').height(50).backgroundColor(Color.Green)Row() {}.width('80%').height(50).backgroundColor(Color.Orange)Row() {}.width('80%').height(50).backgroundColor(Color.Yellow)Row() {}.width('80%').height(50).backgroundColor(Color.Blue)Row() {}.width('80%').height(50).backgroundColor(Color.Red)}.width('100%').alignItems(HorizontalAlign.Center)}
}

效果:
在这里插入图片描述
Row(行)

@Entry
@Component
struct Index {build() {Row({space:20}) {Column() {}.width('15%').height(50).backgroundColor(Color.Red);Column() {}.width('15%').height(50).backgroundColor(Color.Orange);Column() {}.width('15%').height(50).backgroundColor(Color.Red);Column() {}.width('15%').height(50).backgroundColor(Color.Blue);Column() {}.width('15%').height(50).backgroundColor(Color.Pink);}.width('100%').padding(20).backgroundColor('#ccc')}
}

在这里插入图片描述
子元素排列与对齐
● 主轴:线性布局容器在布局方向上的轴线,Row容器主轴为横向,Column容器主轴为纵向。
● 交叉轴:垂直于主轴方向的轴线。Row容器交叉轴为纵向,Column容器交叉轴为横向。
子元素沿主轴方向的排列方式
可以通过justifyContent 属性进行控制,可选值如下:

@Entry
@Component
struct Index {build() {Column({space:20}) {//一行Row() {}.width('80%').height(50).backgroundColor(Color.Green)Row() {}.width('80%').height(50).backgroundColor(Color.Red)}.width('100%').height('100%').justifyContent(FlexAlign.Center)}
}

.justifyContent(FlexAlign.Center)
在这里插入图片描述
.justifyContent(FlexAlign.Start)
在这里插入图片描述
.justifyContent(FlexAlign.End)
在这里插入图片描述
.justifyContent(FlexAlign.SpaceBetween)
在这里插入图片描述
.justifyContent(FlexAlign.SpaceAround)
在这里插入图片描述
.justifyContent(FlexAlign.SpaceEvenly)
在这里插入图片描述
子元素沿交叉轴方向的对齐方式
可以通过alignItems 属性进行控制,可选值如下:

@Entry
@Component
struct Index {build() {Column() {Row() {}.width('80%').height(50).backgroundColor(Color.Red)Row() {}.width('80%').height(50).backgroundColor(Color.Orange)Row() {}.width('80%').height(50).backgroundColor(Color.Yellow)}.width('100%').height('100%').alignItems(HorizontalAlign.Start)}
}

.alignItems(HorizontalAlign.Start)
在这里插入图片描述
.alignItems(HorizontalAlign.Center)
在这里插入图片描述
.alignItems(HorizontalAlign.End)
在这里插入图片描述
**

2、层叠布局(Stack)

Stack布局是一种常用的布局方式,它允许将子元素沿垂直于屏幕的方向堆叠在一起,类似于图层的叠加。子元素可以按照其添加顺序依次叠加在一起,后添加的子元素会覆盖之前添加的子元素,层叠布局具有较强的页面层叠、位置定位能力,其使用场景有广告、卡片层叠效果等。
Stack容器中的子组件可通过zIndex属性设置其所在的层级,zIndex值越大,层级越高,即zIndex值大的组件会覆盖在zIndex值小的组件上方
Stack 布局通常会和 position绝对定位配合使用,设置元素左上角相对于父容器左上角偏移位置配合使用,position语法示例:.position({ x: 180, y: 130 })

@Entry
@Component
struct StackAlign {@State alignment: Alignment = Alignment.Center;build() {Column() {Stack() {Row() {Text('1')}.width(300).height(300).backgroundColor(Color.Yellow)Row() {Text('2')}.width(150).height(150).backgroundColor(Color.Red)Row() {Text('3')}.width(75).height(75).backgroundColor(Color.Green)}}.width('100%')}
}

在这里插入图片描述

.alignContent(Alignment.TopStart)

@Entry
@Component
struct StackAlign {@State alignment: Alignment = Alignment.Center;build() {Column() {Stack() {Row() {Text('1')}.width(300).height(300).backgroundColor(Color.Blue)Row() {Text('2')}.width(150).height(150).backgroundColor(Color.Red)Row() {Text('3')}.width(75).height(75).backgroundColor(Color.Yellow)}.width('100%').backgroundColor('#ccc').alignContent(Alignment.TopStart)    }.width('100%')}
}

在这里插入图片描述
.alignContent(Alignment.TopEnd)
在这里插入图片描述
.alignContent(Alignment.Top)
在这里插入图片描述
.alignContent(Alignment.Start)
在这里插入图片描述
.alignContent(Alignment.Center)
在这里插入图片描述
.alignContent(Alignment.End)
在这里插入图片描述
.alignContent(Alignment.BottomStart)
在这里插入图片描述
.alignContent(Alignment.BottomEnd)
在这里插入图片描述
.alignContent(Alignment.Bottom)
在这里插入图片描述
**

3、网格布局(Grid)

**
网格布局(Grid)是一种强大的页面排版方式,通过将页面划分为行和列组成的网格,使得子组件可以在这个二维网格中自由定位。网格布局的容器组件为Grid,子组件为GridItem,如下图所示。
用1fr来表示占1个’单位‘

@Entry
@Component
struct Index {build() {Grid(){GridItem(){}.backgroundColor(Color.Red)GridItem(){}.backgroundColor(Color.Green)GridItem(){}.backgroundColor(Color.Yellow)GridItem(){}.backgroundColor(Color.Brown)GridItem(){}.backgroundColor(Color.Orange)GridItem(){}.backgroundColor(Color.Black)GridItem(){}.backgroundColor(Color.Orange)GridItem(){}.backgroundColor(Color.Gray)GridItem(){}.backgroundColor(Color.Pink)}.width('100%').height(400).rowsTemplate('1fr 2fr 1fr').columnsTemplate('1fr 1fr 1fr').rowsGap(10).columnsGap(10)}
}

.rowsTemplate(‘1fr 2fr 1fr’)
在这里插入图片描述
.columnsTemplate(‘1fr 2fr 1fr’)
在这里插入图片描述
.rowStart(1).rowEnd(2)
在这里插入图片描述
.rowsGap(10).columnsGap(30)
在这里插入图片描述
当显示内容超出显示区域时,有滚动效果

4、列表布局(List)

列表(List)是一种复杂的容器组件,使用列表可以轻松高效地显示结构化、可滚动的列表信息。列表布局的容器组件为List,子组件为ListItem或者ListItemGroup,其中,ListItem表示单个列表项,ListItemGroup用于列表数据的分组展示,其子组件也是ListItem,如下图所示
.listDirection(Axis.Vertical)

@Entry
@Component
struct Index {build() {List({space:10}) {ListItem() {Text('list1')}.width('100%').backgroundColor(Color.Red)ListItemGroup() {ListItem() {Text('list2')}.width('100%')ListItem() {Text('list3')}.width('100%')}.width('100%').backgroundColor(Color.Yellow)}.width('100%').listDirection(Axis.Vertical)}
}

在这里插入图片描述
.listDirection(Axis.Horizontal)
在这里插入图片描述
.alignListItem(ListItemAlign.End)
在这里插入图片描述
.alignListItem(ListItemAlign.Start)
在这里插入图片描述
.alignListItem(ListItemAlign.Center)
在这里插入图片描述
scrollBar属性可控制滚动条样式

@Entry
@Component
struct Index {@State contactsGroups: object[] = [{title: 'A',contacts: ['赵云','李白','王思'],},{title: 'B',contacts: ['白叶','伯乐'],},{title: 'C',contacts: ['王大','张三'],},{title: 'D',contacts: ['白龙','小明'],},{title: 'E',contacts: ['盖伦','石头','光辉'],}]@Builder Header(item){Text(item.title).fontSize(30).backgroundColor('#ccc').width('100%')}build() {List(){ForEach(this.contactsGroups,(item)=>{ListItemGroup({header:this.Header(item)}){ForEach(item.contacts,(user)=>{ListItem(){Text(user)}.width('100%').height(50)})}},item=>JSON.stringify(item));}.width('100%').height(300).scrollBar(BarState.On)}
}

在这里插入图片描述
以上就是常用布局

关注’猿来编码‘,微信订阅号,回复 ’布局‘,获取

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

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

相关文章

【数据结构七】堆与PriorityQueue详解

堆 在Java中有一种数据结构基于队列,并保证操作的数据带有优先级,该数据结构应该提供了两个最基本的操作,一个是返回最高优先级对象,一个是添加新的对象。这种数据结构就是优先级队列(Priority Queue)。它的底层使用了堆这种数据结…

数学与计算机(2)- 线性代数

原文:https://blog.iyatt.com/?p13044 1 矩阵 NumPy 中 array 和 matrix 都可以用于储存矩阵,后者是前者的子类,array 可以表示任意维度,matrix 只能是二维,相当于矩阵专用,在一些矩阵的运算操作上较为直…

openEular入门操作(仅供学习哦~)

如何登录Linux? shell简介 修改密码 Linux用户 bash shell快捷操作 进入openEular,输入账号密码。 命令ip addr 进入putty,输入查到的ip,使用ssh。即可远程登录 修改密码, 修改用户(passwd 用户名字&…

Secure MIMO Communication Relying on Movable Antennas

文章目录 IV. SIMULATION RESULTSA. Simulation SetupB. Convergence Behaviour of the Proposed AlgorithmsC. Baseline SchemesD. Performance Analysis Compared to Baseline Schemes IV. SIMULATION RESULTS 在本节中,仿真结果验证了所提出算法的有效性&#x…

Python 多线程大批量处理文件小程序

说明 平时偶尔需要进行重复性的对文件进行重命名、格式转化等。假设以文件复制功能作为目标,设计一个小程序使用多线程对文件进行批量复制。(其实以后主要目标是针对Realsense的raw文件进行批量的转化,并借助多线程加速) 代码 i…

FileZillaClient连接被拒绝,无法连接

1.ECONNREFUSED - 连接被服务器拒绝 2、无法连接FZ时,判断没有ssh 更新源列表: sudo apt-get update 安装 openssh-server :sudo apt-get install openssh-server 查看是否启动ssh:sudo ps -e | grep ssh

html5cssjs代码 023 公制计量单位进位与换算表

html5&css&js代码 023 公制计量单位进位与换算表 一、代码二、解释 这段HTML代码定义了一个网页&#xff0c;用于展示公制计量单位的进位与换算表。 一、代码 <!DOCTYPE html> <html lang"zh-cn"> <head><meta charset"utf-8&quo…

【全面了解自然语言处理三大特征提取器】RNN(LSTM)、transformer(注意力机制)、CNN

目录 一 、RNN1.RNN单个cell的结构2.RNN工作原理3.RNN优缺点 二、LSTM1.LSTM单个cell的结构2. LSTM工作原理 三、transformer1 Encoder&#xff08;1&#xff09;position encoding&#xff08;2&#xff09;multi-head-attention&#xff08;3&#xff09;add&norm 残差链…

OpenHarmony4.0对RK3566的烧写过程

前面已经编译的过程搞了比较长的时间,因为遇到了不少问题,老是编译出错,后来经过努力还是编译成功了。 我这里主要针对RK3566的Purple Pi OH开发板,如下图: 因为开源鸿蒙里没有针对这个板的特殊配置,需要下载下面这个文件: purple-pi-oh-patch.zip 这个文件里包含了可…

暄桐二期《集字圣教序》21天教练日课又跟大家见面啦

林曦老师的直播课&#xff0c;是暄桐教室的必修课。而教练日课是丰富多彩的选修课&#xff0c;它会选出书法史/美术史上重要的、有营养的碑帖和画儿&#xff0c;与你一起&#xff0c;高效练习。而且暄桐教练日课远不止书法、国画&#xff0c;今后还会有更多有趣的课程陆续推出&…

NSSCTF 403,444,2145,3845,404,445

[SWPUCTF 2021 新生赛]简简单单的逻辑 py文件&#xff0c;使用pycharm打开进行分析 其中&#xff0c;hex()[2:]&#xff1a;将十进制转化为十六进制 zfill(2)&#xff1a;位数不足2&#xff0c;前补0 这里即将flag的ASCII码与key进行异或&#xff0c;再将每位转化为十六进制…

蓝桥杯第 6 场 小白入门赛 2.猜灯谜(for + 数组)

思路&#xff1a;注意是环形排列的灯笼&#xff0c;它的谜底是相邻两个灯笼的数字之和。这道题要用到两个数组&#xff0c;ans存答案&#xff0c;a存原数据。数据读入部分就不用说了&#xff0c;重点就是单独写明ans[0]和ans[n-1]两个取值&#xff0c;其他的用for循环数组就可以…

AtCoder Beginner Contest 345 A - E 题解

A - Leftrightarrow 思路 判断第一个字符是否为&#xff0c;最后一个字符是否为&#xff0c;都满足的话&#xff0c;再判断中间字符是否都为 代码 #include<iostream> using namespace std; #define int long longbool check(string s){int ns.size();if(s[0]!<) …

Jz32从上往下打印二叉树

//add()和remove()方法在失败的时候会抛出异常(不推荐) // 用offer 和poll 替代 import java.util.ArrayList; import java.util.*; /** public class TreeNode {int val 0;TreeNode left null;TreeNode right null;public TreeNode(int val) {this.val val;}} */ public …

Oracle 部署及基础使用

1. Oracle 简介 Oracle Database&#xff0c;又名 Oracle RDBMS&#xff0c;简称 Oracle Oracle系统&#xff0c;即是以Oracle关系数据库为数据存储和管理作为构架基础&#xff0c;构建出的数据库管理系统。是目前最流行的客户/服务器&#xff08;client/server&#xff09;或…

洛谷P8972 『GROI-R1』 一切都已过去(树上前缀和+运算符重载)

『GROI-R1』 一切都已过去 题目背景 悦关上窗&#xff0c;拉上帘布。 果然还是想不起来啊。 隐约记得曾和什么人一起做过这样的事。 仰面躺下&#xff0c;手执一只木笺。 「究竟如何&#xff0c;才能拥有“过去”啊……」 她闭上双眼。 「6 岁前的记忆……究竟如何才能…

十、MySQL主从架构配置

一、资源配置 主库&#xff1a;192.168.134.132 从库&#xff1a;192.168.134.133 从库&#xff1a;192.168.134.134 二、主从同步基本原理&#xff1a; master用户写入数据&#xff0c;会生成event记录到binary log中&#xff0c;slave会从master读取binlog来进行数据同步…

<商务世界>《第12课 发票种类和增值税专用发票的税率》

1 增值税发票类型 分为增值税发票和增值税专用发票&#xff0c;都是税务部门为了管理增值税而设立的重要工具&#xff0c;但它们在使用范围、功能以及具体的格式等方面存在明显的区别。 1.1 增值税发票 是一种广泛使用的税务凭证&#xff0c;它涵盖了多种类型的发票&#xf…

VPTTA:为每张医疗图像生成特定的“提示”,解决跨不同设备和条件的医疗图像分割的准确性和适应性

VPTTA&#xff1a;为每张医疗图像生成特定的“提示”&#xff0c;解决跨不同设备和条件的医疗图像分割的准确性和适应性 提出背景VPTTA 方法VPTTA 步骤 提出背景 论文&#xff1a;https://arxiv.org/pdf/2311.18363.pdf 代码&#xff1a;https://github.com/Chen-Ziyang/VPTT…

STM32输入捕获模式测频率

STM32频率的测量&#xff1a;高频适合使用的方法是测频法&#xff0c;低频适合使用的是测周法&#xff0c;&#xff08;其中使用测频法测量频率比较稳定&#xff0c;使用测周法测量频率的方式没有这么稳定&#xff0c;因为测周法只会通过一次的测量就能得出结果所以测试出来的频…