当前位置: 首页 > news >正文

鸿蒙5:布局组件

注意:博主有个鸿蒙专栏,里面从上到下有关于鸿蒙next的教学文档,大家感兴趣可以学习下

如果大家觉得博主文章写的好的话,可以点下关注,博主会一直更新鸿蒙next相关知识

专栏地址: https://blog.csdn.net/qq_56760790/category_12794123.html

文章所属类目(HarmonyOS 语言-ArkTS)

目录

1. 布局

1.1 基础线性布局

1.1.1 基本介绍

1.1.2 语法

1.1.3 属性

Row

Column

1.1.4 用法

1.1.5 总结

1.2 线性布局小案例-练习题

1.3 线性布局小案例-百度首页

1.4 堆叠布局

1.4.1 基本介绍

1.4.2 语法

1.4.3 用法


1. 布局

1.1 基础线性布局

参考地址

文档中心

1.1.1 基本介绍

Row 和Column的布局方式成为线性布局- 不是横向排列就是纵向排列

Row:沿水平方向布局容器

Column:沿垂直方向布局的容器

1.1.2 语法

Row(value?:{space?: string | number })

参数名

类型

必填

说明

value

{space?: string | number }

横向布局元素间距。

Column(value?: {space?: string | number})

参数名

类型

必填

说明

value

{space?: string | number}

纵向布局元素垂直方向间距。

1.1.3 属性
Row

横向布局-采用Row

  • alignItems 设置子元素在交叉轴方向的对齐格式

语法 alignItems(value: VerticalAlign)

VerticalAlign参数枚举

  • justifyContent 设置子组件在水平方向上的对齐格式

语法 justifyContent(value: FlexAlign)

FlexAlign参数枚举

Column

纵向布局-采用column

  • alignItems 设置子组件在水平方向上的对齐格式

语法:alignItems(value: HorizontalAlign)

参数HorizontalAlign:子组件在水平方向上的对齐格式。默认值:HorizontalAlign.Center

  • justifyContent 设置子组件在垂直方向上的对齐格式

语法:justifyContent(value: FlexAlign)

参数FlexAlign:子组件在垂直方向上的对齐格式。默认值:FlexAlign.Start

总结:属性justifyContent 设置子元素在主轴方向的对其格式 参数是 FlexAlign枚举

属性alignItems 设置子元素在交叉轴(垂直主轴方向的轴)方向的对齐格式 Row 容器使用VerticalAlign枚举 Column容器使用HorizontalAlign枚举

1.1.4 用法
  • 横向布局
@Entry@Componentstruct Index {build() {Column() {Row({space:15}){Text('第一个').width(100).height(100).backgroundColor(Color.Blue)Text('第二个').width(100).height(100).backgroundColor(Color.Yellow)Text('第三个').width(100).height(100).backgroundColor(Color.Pink)}.width('100%').height('100%').justifyContent(FlexAlign.Center).alignItems(VerticalAlign.Center)}.width('100%').height('100%')}}

  • 纵向布局
@Entry@Componentstruct Index {build() {Column() {Column({space:15}){Text('第一个').width(100).height(100).backgroundColor(Color.Blue)Text('第二个').width(100).height(100).backgroundColor(Color.Yellow)Text('第三个').width(100).height(100).backgroundColor(Color.Pink)}.width('100%').height('100%').justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Center)}.width('100%').height('100%')}}
1.1.5 总结

Row 和Column的布局方式成为线性布局- 不是横向排列就是纵向排列

  • 线性布局中永远不会产生换行
  • 均不支持出现滚动条
  • 横向排列的垂直居中,总行排列的水平居中
  • 主轴-排列方向的轴
  • 侧轴-排列方向垂直的轴

1.2 线性布局小案例-练习题

@Entry@Componentstruct Exercise {build() {Row({ space: 10 }) {Row({ space: 2 }) {Text('500').fontWeight(FontWeight.Bold)Text('总访问量')}Row({ space: 2 }) {Text('120').fontWeight(FontWeight.Bold)Text('原创')}Row({ space: 2 }) {Text('200').fontWeight(FontWeight.Bold)Text('排名')}Row({ space: 2 }) {Text('100').fontWeight(FontWeight.Bold)Text('粉丝')}}.justifyContent(FlexAlign.SpaceEvenly).width('100%').height(60)}}

1.3 线性布局小案例-百度首页

@Entry@Componentstruct Baidu {build() {Column() {Image('https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png').width(160)Row() {TextInput().layoutWeight(2).borderRadius({topLeft: 6,bottomLeft: 6}).backgroundColor('#fff').border({ width: 2, color: '#ccc' }).height(40)Button('百度一下').layoutWeight(1).type(ButtonType.Normal).borderRadius({topRight: 6,bottomRight: 6}).backgroundColor('#3274f6')}.padding(20)}.width('100%').height('100%')}}

1.4 堆叠布局

参考地址

文档中心

1.4.1 基本介绍

层叠布局通过Stack容器组件实现位置的固定定位与层叠,容器中的子元素依次入栈,后一个子元素覆盖前一个子元素,子元素可以叠加,也可以设置位置。

层叠布局具有较强的页面层叠、位置定位能力,其使用场景有广告、卡片层叠效果等。

1.4.2 语法

Stack(value?: { alignContent?: Alignment })

1.4.3 用法

@Entry@Componentstruct StackDemo {build() {Column() {Stack({alignContent:Alignment.TopEnd}){Image('https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png').width(160)Text('鸿蒙版').translate({y: 20})}Row() {TextInput().layoutWeight(2).borderRadius({topLeft: 6,bottomLeft: 6}).backgroundColor('#fff').border({ width: 2, color: '#ccc' }).height(40)Button('百度一下').layoutWeight(1).type(ButtonType.Normal).borderRadius({topRight: 6,bottomRight: 6}).backgroundColor('#3274f6')}.padding(20)}.width('100%').height('100%')}}

Stack的参数 可以设置子组件的排列方式-alignContent

  • Top(顶部)
  • TopStart(左上角)
  • TopEnd(右上角)
  • Start(左侧)
  • End(右侧)
  • Center(中间)
  • Bottom(底部)
  • BottomStart(左下角)
  • BottomEnd(右下角)
http://www.lryc.cn/news/576502.html

相关文章:

  • docker通过小实例使用常用命令
  • 能否仅用两台服务器实现集群的高可用性??
  • 【算法深练】单调栈:有序入栈,及时删除垃圾数据
  • 嵌入式网络通信与物联网协议全解析:Wi-Fi、BLE、LoRa、ZigBee 实战指南
  • libxlsxwriter: 一个轻量级的跨平台的C++操作Excel的开源库
  • 【HarmonyOS NEXT】跳转到华为应用市场进行应用下载并更新
  • COLT_CMDB_linux_zookeeperInfo_20250628.sh
  • cocos creator 3.8 - 精品源码 -《文字大师》(移一笔变新字)
  • Insar 相位展开真实的数据集的生成与下载(随机矩阵放大,zernike 仿真包裹相位)
  • Cesium快速入门到精通系列教程十一:Cesium1.74中高性能渲染上万Polyline
  • SLAM中的非线性优化-2D图优化之零空间(十五)
  • 变长字节的数字表示法vb224
  • 互联网大厂Java求职面试实录
  • c# sugersql 获取子表数据排序
  • Java 识别和处理 HTML 标签内容
  • Spring MVC参数解析:深入剖析415异常与@RequestBody处理机制问题场景
  • Flutter基础(FFI)
  • pytorch中的几个概念
  • NLP中的同义词替换及我踩的坑
  • 《Python 实现 B 站视频信息爬虫:从批量获取到 CSV 保存》
  • 数字孪生技术引领UI前端设计新革命:实时交互与模拟预测
  • LINUX628 NFS 多web;主从dns;ntp;samba
  • 鸿蒙5:ArkTS基本介绍
  • VR训练美国服务器:高性能解决方案与优化指南
  • 【LeetCode 热题 100】438. 找到字符串中所有字母异位词——(解法三)不定长滑动窗口+数组
  • 构建 AI 系统的 4 大 Agentic AI 设计模式
  • 网关ARP防护的措施
  • qt和qtcreator版本关系
  • n8n-nodes-puppeteer截图中文变方块乱码解决方法
  • 在单片机中如何实现一个shell控制台