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

6.ArkUI Row的介绍和使用

ArkUI Row 组件介绍与使用指南

什么是 Row 组件?

Row 是 ArkUI 中的基础布局容器组件,用于水平(横向)排列子组件。它与 Column 组件相对应,是构建用户界面最常用的布局方式之一,类似于其他UI框架中的水平线性布局。

Row 的基本属性

  1. space:设置子组件之间的水平间距
  2. alignItems:设置子组件在垂直方向的对齐方式
    • VerticalAlign.Top(默认):顶部对齐
    • VerticalAlign.Center:垂直居中
    • VerticalAlign.Bottom:底部对齐
  3. justifyContent:设置子组件在水平方向的对齐方式
    • FlexAlign.Start(默认):左对齐
    • FlexAlign.Center:水平居中
    • FlexAlign.End:右对齐
    • FlexAlign.SpaceBetween:均匀分布,首尾不留空
    • FlexAlign.SpaceAround:均匀分布,首尾留空
    • FlexAlign.SpaceEvenly:完全均匀分布

基本使用方法

@Entry
@Component
struct RowExample {build() {Row({ space: 20 }) {Text('第一个').fontSize(20).backgroundColor('#f0f0f0').height(60).textAlign(TextAlign.Center).layoutWeight(1)Text('第二个').fontSize(20).backgroundColor('#e0e0e0').height(60).textAlign(TextAlign.Center).layoutWeight(1)Text('第三个').fontSize(20).backgroundColor('#d0d0d0').height(60).textAlign(TextAlign.Center).layoutWeight(1)}.width('100%').height(100).margin({ top: 20 }).backgroundColor('#ffffff')}
}

高级用法

设置对齐方式

Row() {// 子组件...
}
.width('100%')
.height(100)
.alignItems(VerticalAlign.Center) // 垂直居中
.justifyContent(FlexAlign.SpaceBetween) // 水平方向均匀分布
.border({ width: 1, color: Color.Black })

嵌套使用

Row({ space: 10 }) {Column() {Text('列1')Text('列1')}.layoutWeight(1)Row({ space: 5 }) {Text('嵌套行1')Text('嵌套行2')}.layoutWeight(1).margin({ left: 10 }).border({ width: 1, color: Color.Gray })
}

与 Scroll 结合实现水平滚动

@State itemList: string[] = ['项目1', '项目2', '项目3', '项目4', '项目5', '项目6', '项目7', '项目8']build() {Scroll() {Row({ space: 15 }) {ForEach(this.itemList, (item: string) => {Text(item).fontSize(18).height(80).width(120).backgroundColor('#f5f5f5').textAlign(TextAlign.Center)})}.height(100).padding(10)}.scrollable(ScrollDirection.Horizontal) // 水平滚动.margin({ top: 20 })
}

权重布局 (layoutWeight)

Row() {Text('权重1').backgroundColor('#ffcccc').height(60).layoutWeight(1) // 占据剩余空间的1/3Text('权重2').backgroundColor('#ccffcc').height(60).layoutWeight(2) // 占据剩余空间的2/3
}
.width('100%')
.margin(20)

实际应用示例

底部导航栏

@Entry
@Component
struct BottomNav {@State currentIndex: number = 0build() {Column() {// 内容区域Text(`当前页面: ${this.currentIndex + 1}`).fontSize(24).margin(30)// 底部导航栏Row() {ForEach([0, 1, 2], (index: number) => {Column() {Image(this.currentIndex === index ? 'selected_icon' : 'normal_icon').width(24).height(24)Text(`页面${index + 1}`).fontSize(12).margin({ top: 4 })}.onClick(() => {this.currentIndex = index}).layoutWeight(1).alignItems(HorizontalAlign.Center)})}.width('100%').height(60).backgroundColor('#f8f8f8').border({ width: 1, color: '#eeeeee' })}.width('100%').height('100%').justifyContent(FlexAlign.SpaceBetween)}
}

商品卡片布局

@Entry
@Component
struct ProductCard {build() {Column() {// 商品图片Image('product_image').width('100%').height(200).objectFit(ImageFit.Cover)// 商品信息行Row() {Column() {Text('商品名称').fontSize(16).fontWeight(FontWeight.Bold)Text('¥199.00').fontSize(14).margin({ top: 4 }).fontColor('#ff0000')}.layoutWeight(1)Button('购买').width(80).height(36).fontSize(14)}.padding(10).alignItems(VerticalAlign.Center)}.width(180).borderRadius(8).backgroundColor(Color.White).shadow({ radius: 4, color: '#1a000000', offsetX: 1, offsetY: 1 }).margin(10)}
}

注意事项

  1. Row 默认会占用所有可用宽度,如果不希望这样,需要明确设置其宽度
  2. 子组件默认会占据 Row 的整个可用高度
  3. 当内容超出容器宽度时,需要配合 Scroll 组件使用才能实现水平滚动
  4. 使用 layoutWeight 时,所有设置了该属性的子组件会按比例分配剩余空间
  5. 在性能敏感的场景中,避免过度嵌套 Row 和 Column 组件

Row 组件是 ArkUI 中实现水平布局的核心组件,熟练掌握它的使用可以高效地构建各种用户界面布局,特别是导航栏、按钮组、列表项等常见UI元素。

http://www.xdnf.cn/news/2004.html

相关文章:

  • 笔记:记一次使用EasyExcel重写convertToExcelData方法无法读取@ExcelDictFormat注解的问题(已解决)
  • 计算机视觉各类任务评价指标详解
  • 8. 深入Spring AI:自定义Advisor
  • 反爬策略应对指南:淘宝 API 商品数据采集的 IP 代理与请求伪装技术
  • OceanBase 复合索引指南
  • 项目maven版本不一致 导致无法下载
  • 人工智能与机器学习:Python从零实现性回归模型
  • 从“能耗大户”到“节能标杆”:安科瑞助力污水处理厂绿色转型
  • 告别进度失控:用燃尽图补上甘特图的监控盲区
  • Windows server:
  • [OS_8] 终端和 UNIX Shell | 会话和进程组 | sigaction | dash
  • 多模态大语言模型(MLLM)- kimi-vl technical report论文阅读
  • 航电系统之自适应航电修复机制篇
  • Flowable7.x学习笔记(十四)查看部署流程Bpmn2.0-xml
  • TestBrain开源程序是一款集使用AI(如deepseek)大模型自动生成测试用例、和测试用例评审、RAG知识库管理的web平台系统
  • 解读《地方标准制定负面清单》与安徽标准复审新规
  • 蜜罐管理和数据收集服务器:Modern Honey Network (MHN)
  • 成熟的前端vue vite websocket,Django后端实现方案包含主动断开websocket连接的实现
  • 企业部署Power BI 报表服务器,在第三方系统嵌套该报表服务器,并实现单点登录
  • 【数据可视化艺术·应用篇】三维管线分析如何重构城市“生命线“管理?
  • 基于Java,SpringBoot,Vue,HTML宠物相亲配对婚恋系统设计
  • 【Python数据库与后端开发】从ORM到RESTful API
  • Dubbo(76)Dubbo的服务注册与发现机制是怎样的?
  • c#接口_抽象类_多态学习
  • RabbitMQ 复习总结
  • 【MFC】 VS2022打开低版本的MFC,双击.rc文件,DIalog加载失败,页面弹窗fatal error RC***:cannot open*****
  • Matplotlib高阶技术全景解析(续):动态交互、三维可视化与性能优化
  • 大模型助力嘉兴妇幼:数据分类分级的智能化飞跃
  • 7.11 Python CLI开发实战:API集成与异步处理核心技术解析
  • flume整合kafka