【HarmonyOS】ArkUI 布局与容器组件

目录

  • 前言
  • 一、线性布局(Column/Row)
    • 1.先布局后内容
    • 2.元素在主轴上的排列方式
    • 3.元素在交叉轴上的排列方式
  • 二、层叠布局(Stack)
    • 1.开发布局
    • 2.对齐方式
  • 三、弹性布局(Flex)
  • 四、创建列表(List)
  • 五、创建轮播(Swiper)
    • 1.基本用法
    • 2.常用属性
    • 3.样式自定义
  • 六、选项卡Tabs
    • 1.基本用法
    • 2.常用属性
    • 3.滚动导航栏
    • 4.自定义TabBar
    • 5.自定义TabBar-高亮切换
    • 小米有品案例

前言

在 HarmonyOS 应用开发中,优秀的界面布局是用户体验的基础。ArkUI 作为新一代声明式 UI 开发框架,提供了丰富多样的布局组件和容器组件,帮助开发者高效构建复杂界面。本文将系统性地介绍 ArkUI 的核心布局方式,包括线性布局、层叠布局、弹性布局等基础布局,以及列表、轮播、选项卡等高级容器组件。

一、线性布局(Column/Row)

1.先布局后内容

容器组件:布局
Colum {} 内容会竖着排
Row() {} 内容会横着排
内容组件:内容
Text(‘内容’)

@Entry
@Component
struct Index {build() { //里面要有唯一的容器根组件Column() {Column() {Text('大壮')Text('中壮');Text('小壮');}Row() {Text('大壮')Text('中壮');Text('小壮');}}}
}

在这里插入图片描述

2.元素在主轴上的排列方式

(1) Column:justifyContent(FlexAlign.xxx)

Column({}) {Column() {}.width('80%').height(50).backgroundColor(0xF5DEB3)Column() {}.width('80%').height(50).backgroundColor(0xD2B48C)Column() {}.width('80%').height(50).backgroundColor(0xF5DEB3)
}.width('100%').height(300).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.Start)

Start
在这里插入图片描述
End
在这里插入图片描述

(2) Row:justifyContent(FlexAlign.xxx)

Row({}) {Column() {}.width('20%').height(30).backgroundColor(0xF5DEB3)Column() {}.width('20%').height(30).backgroundColor(0xD2B48C)Column() {}.width('20%').height(30).backgroundColor(0xF5DEB3)
}.width('100%').height(200).backgroundColor('rgb(242,242,242)').justifyContent(FlexAlign.Start)

在这里插入图片描述

3.元素在交叉轴上的排列方式

(1)Column:alignItems(HorizontalAlign.Start)

Column({}) {Column() {}.width('80%').height(50).backgroundColor(0xF5DEB3)Column() {}.width('80%').height(50).backgroundColor(0xD2B48C)Column() {}.width('80%').height(50).backgroundColor(0xF5DEB3)
}.width('100%').alignItems(HorizontalAlign.Start).backgroundColor('rgb(242,242,242)') //右对齐

在这里插入图片描述

(2)Row:

Row({}) {Column() {}.width('20%').height(30).backgroundColor(0xF5DEB3)Column() {}.width('20%').height(30).backgroundColor(0xD2B48C)Column() {}.width('20%').height(30).backgroundColor(0xF5DEB3)//上对齐
}.width('100%').height(200).alignItems(VerticalAlign.Top).backgroundColor('rgb(242,242,242)')

在这里插入图片描述

二、层叠布局(Stack)

1.开发布局

Stack组件为容器组件,容器内可包含各种子元素。其中子元素默认进行居中堆叠。子元素被约束在Stack下,进行自己的样式定义以及排列。

// xxx.ets
let MTop:Record<string,number> = { 'top': 50 }
@Entry
@Component
struct StackExample {build() {Column(){Stack({ }) {Column(){}.width('90%').height('100%').backgroundColor('#ff58b87c')Text('text').width('60%').height('60%').backgroundColor('#ffc3f6aa')Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000')}.width('100%').height(150).margin(MTop)}}
}

在这里插入图片描述

2.对齐方式

Stack组件通过alignContent参数实现位置的相对移动。如图2所示,支持九种对齐方式。

@Entry
@Component
struct StackExample {build() {Stack({ alignContent: Alignment.TopStart }) {Text('Stack').width('90%').height('100%').backgroundColor('#e1dede').align(Alignment.BottomEnd)Text('Item 1').width('70%').height('80%').backgroundColor(0xd2cab3).align(Alignment.BottomEnd)Text('Item 2').width('50%').height('60%').backgroundColor(0xc1cbac).align(Alignment.BottomEnd)}.width('100%').height(150).margin({ top: 5 })}
}

在这里插入图片描述

三、弹性布局(Flex)

弹性布局(Flex)提供更加有效的方式对容器中的子元素进行排列、对齐和分配剩余空间。常用于页面头部导航栏的均匀分布、页面框架的搭建、多行数据的排列等。

容器默认存在主轴与交叉轴,子元素默认沿主轴排列,子元素在主轴方向的尺寸称为主轴尺寸,在交叉轴方向的尺寸称为交叉轴尺寸。
在这里插入图片描述

@Entry
@Component
struct Index {build() {// Flex默认主轴水平往右,交叉轴垂直往下 -> Row// 1. 主轴方向 通过direction: FlexDirection.Row/Column来调整// 2. 主轴对齐方式 justifyContent// 3. 交叉轴对齐方式 alignItems// 单行或者单列的情况,优先使用线性布局(本质基于Flex设计)Flex({direction: FlexDirection.Column,justifyContent: FlexAlign.SpaceEvenly,alignItems: ItemAlign.Stretch //拉伸}) {Text().width(80).height(80).backgroundColor(Color.Pink).border({ width: 1, color: Color.Blue })Text().width(80).height(80).backgroundColor(Color.Pink).border({ width: 1, color: Color.Blue })Text().width(80).height(80).backgroundColor(Color.Pink).border({ width: 1, color: Color.Blue })}.width('100%').height(600).backgroundColor('#5f9a5c')}
}

在这里插入图片描述

@Entry
@Component
struct Index {build() {// 4. 换行 wrapFlex({wrap: FlexWrap.Wrap// 换行}) {Text().width(80).height(80).backgroundColor(Color.Pink).border({ width: 1, color: Color.Blue })Text().width(80).height(80).backgroundColor(Color.Pink).border({ width: 1, color: Color.Blue })Text().width(80).height(80).backgroundColor(Color.Pink).border({ width: 1, color: Color.Blue })Text().width(80).height(80).backgroundColor(Color.Pink).border({ width: 1, color: Color.Blue })Text().width(80).height(80).backgroundColor(Color.Pink).border({ width: 1, color: Color.Blue })}.width(300).height(300).backgroundColor('#5f9a5c')}
}

案例:
在这里插入图片描述

let names: string[] =['ArkUI', 'ArkTS', '界面开发', '系统能力', '权限控制', '元服务']@Entry
@Component
struct Index {build() {Column() {Text('阶段选择').fontSize(30).fontWeight(700).padding(15).width('100%')Flex({wrap: FlexWrap.Wrap}) {ForEach(names, (item: string) => {Text(item).padding(10).margin(5).backgroundColor('#f1f1f1')})}}}
}

四、创建列表(List)

列表是一种容器,当列表项达到一定数量,超过List容器组件大小,自动滚动

主轴方向(垂直).listDirection(Axis.Horizontal)
交叉轴布局lane(列数,间距)
列对齐方式.alignListItem(ListItemAlign.Center)
滚动条状态.scrollBar(Barstate.Auto)
分割线样式divider({…})
@Entry
@Component
struct Index {build() {Column() {List() {ForEach(Array.from({length: 20}),() => {ListItem() {Row() {}.width('100%').height(100).backgroundColor(Color.Brown)}.padding(10)})}.width('100%').layoutWeight(1).backgroundColor(Color.Orange).listDirection(Axis.Vertical) //调整主轴方向.lanes(1, 5) //调整列数和间距.alignListItem(ListItemAlign.Center).scrollBar(BarState.Auto) //按需自动显示滚动条.divider({strokeWidth: 3, //线宽color: Color.Blue, //颜色startMargin: 10, //左边线距离边缘的间隙endMargin: 10 //右边线距离边缘的间隙})}.width('100%').height('100%')}
}

在这里插入图片描述
lanes(2, 5)
在这里插入图片描述

五、创建轮播(Swiper)

1.基本用法

Swiper() {// 1.轮播内容
}
// 2.设置尺寸(内容会自动拉伸)
.width('100%')
.height(100)
@Entry
@Component
struct Index {build() {Column() {Swiper() {Text('1').backgroundColor(Color.Orange)Text('2').backgroundColor(Color.Yellow)Text('3').backgroundColor(Color.Pink)}.loop(true) // 是否开启循环.autoPlay(true) // 是否自动播放.interval(2000) // 自动播放的间隔时间.vertical(false) // 横向.width('100%').height(100)Swiper() {Image($r('app.media.syt'))Image($r('app.media.wave'))}.width('100%').height(400)}}
}

在这里插入图片描述

2.常用属性

属性方法传值作用默认值
loopboolean是否开启循环true
autoPlayboolean是否自动播放false
intervalnumber自动播放的时间间隔3000
verticalboolean纵向滑动轮播false

3.样式自定义

Swiper() {
}
.aspectRatio(2) //宽高比为2
.indicator(Indicator.dot()// 小圆点.itemWidth(20)// 默认的宽.itemHeight(20)//默认的高.color(Color.Black)// 默认的颜色.selectedItemWidth(30)//选中的宽.selectedItemHeight(30)// 选中的高.selectedColor(Color.White)// 选中的颜色)

六、选项卡Tabs

1.基本用法

@Entry
@Component
struct Index {build() {Tabs() {TabContent() {Text('首页内容') //有且一个子组件}.tabBar('首页')TabContent() {Text('推荐内容')}.tabBar('推荐')TabContent() {Text('发现内容')}.tabBar('发现')TabContent() {Text('我的内容')}.tabBar('我的')}}
}

2.常用属性

名称作用
barPostion调整位置 :开头 或 结尾(参数)
vertical调整导航 :水平 或 垂直
scrollable调整是否:手势滑动 切换
animationDuration点击滑动动画时间
@Entry
@Component
struct Index {build() {Tabs({barPosition: BarPosition.Start}) {TabContent() {Text('首页内容') //有且一个子组件}.tabBar('首页')TabContent() {Text('推荐内容')}.tabBar('推荐')TabContent() {Text('发现内容')}.tabBar('发现')TabContent() {Text('我的内容')}.tabBar('我的')}.vertical(false) //选项横向排布.scrollable(false) //false为不支持手势滑动.animationDuration(0) //点击动画时间为0毫秒}
}

在这里插入图片描述

3.滚动导航栏

如果导航栏的内容较多,屏幕无法容纳时,可以将他设置为滚动
可以通过Tabs组件的barMode属性即可调整为固定导航栏或滚动导航栏

@Entry
@Component
struct Index {titles: string[] = ['首页', '关注', '热门', '军事', '体育','八卦', '数码', '财经', '美食', '旅行']build() {Tabs() {ForEach(this.titles, (item: string, index) => {TabContent() {Text(`${item}内容`)}.tabBar(item)})}.barMode(BarMode.Scrollable)//滚动导航}
}

在这里插入图片描述

4.自定义TabBar

@Entry
@Component
struct Index {@Builder myBuilder (title: string, img: Resource) {Column() {Image(img).width(30)Text(title)}}build() {Tabs({barPosition: BarPosition.End}){TabContent() {Text('购物车')}.tabBar(this.myBuilder('购物车', $r('app.media.ic_tabbar_icon_2')))TabContent() {Text('我的内容')}.tabBar(this.myBuilder('我的', $r('app.media.ic_tabbar_icon_3')))}}
}

[图片]

5.自定义TabBar-高亮切换

核心思路:
1.监听切换事件 -> 得到索引值,记录高亮的索引
2.给每个tabBar起个标记:0,1,2
3.在TabBar内部比较标记 == 记录的索引?高亮:不高亮

名称功能描述
onChange(event:(index: number) => void)Tab页签切换后触发的事件;index:当前显示的index索引,索引从0开始;滑动切换、点击切换均会触发
onTabBarClick(event: (index: number) => void)Tab页签点击后触发的事件

小米有品案例

@Entry
@Component
struct Index {@State indexNumber: number = 0;@Builder myBuilder(index: number, title: string, img1: ResourceStr, img2: ResourceStr) {Column() {Image(this.indexNumber == index? img2: img1).width(20)Text(title).fontColor(this.indexNumber == index? Color.Black: Color.Gray)}}build() {Tabs({barPosition: BarPosition.End}) {TabContent() {Text('首页')}.tabBar(this.myBuilder(0, '首页', $r('app.media.ic_tabbar_icon_0'), $r('app.media.ic_tabbar_icon_0_selected')))TabContent() {Text('分类')}.tabBar(this.myBuilder(1, '分类', $r('app.media.ic_tabbar_icon_1'), $r('app.media.ic_tabbar_icon_1_selected')))TabContent() {Text('购物车')}.tabBar(this.myBuilder(2, '购物车', $r('app.media.ic_tabbar_icon_2'), $r('app.media.ic_tabbar_icon_2_selected')))TabContent() {Text('我的')}.tabBar(this.myBuilder(3, '我的', $r('app.media.ic_tabbar_icon_3'), $r('app.media.ic_tabbar_icon_3_selected')))}.onChange((index: number) => {this.indexNumber = index})}
}

在这里插入图片描述

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

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

相关文章

MCNN-BiLSTM-Attention分类预测模型等!

MCNN-BiLSTM-Attention分类预测模型基于多尺度卷积神经网络(MCNN)双向长短期记忆网络(BiLSTM)注意力机制(Attention)的分类预测模型&#xff0c;matlab代码&#xff0c;直接运行使用&#xff01;1、模型介绍&#xff1a;针对传统方法在噪声环境下诊断精度低的问题&#xff0c;提…

【Luogu】每日一题——Day12. P3149 排序 (树状数组 + 逆序对)

链接&#xff1a;P3149 排序 - 洛谷 题目&#xff1a; 思路&#xff1a; 经典搭配了 首先我们来分析以下操作的作用&#xff0c;如果我们选了 a[k]&#xff0c;那么对逆序对有什么影响呢&#xff1f; ①.对于 x y&#xff0c;且 x > a[k]&#xff0c;y < a[k] 由于 x…

电商项目_秒杀_架构升级

1. 秒杀当前架构设计nginx节点和订单服务都可以方便的扩容&#xff08;增加机器&#xff09;redis扩容需则需要考虑架构设计当前架构面临的痛点&#xff1a;秒杀系统redis是单节点&#xff08;主从&#xff09;部署&#xff0c;读redis时并发量会成为瓶颈。所以考虑将增加redis…

CodeBuddy IDE发布:编程新时代的颠覆者?

开场&#xff1a;编程界的 “新风暴” 来袭 你能想象&#xff0c;不用敲一行代码就能开发软件吗&#xff1f;这个曾经只存在于科幻电影里的场景&#xff0c;如今已经成为现实&#xff01;就在最近&#xff0c;编程界迎来了一场 “新风暴”——CodeBuddy IDE 重磅发布&#xff…

深度分析Java类加载机制

Java 的类加载机制是其实现平台无关性、安全性和动态性的核心基石。它不仅仅是简单地将 .class 文件加载到内存中&#xff0c;而是一个精巧、可扩展、遵循特定规则的生命周期管理过程。以下是对其深度分析&#xff1a; 一、核心概念与生命周期 一个类型&#xff08;Class 或 In…

神经网络实战案例:用户情感分析模型

在当今数字化时代&#xff0c;用户评论和反馈成为企业了解产品满意度的重要渠道。本项目将通过神经网络构建一个情感分析模型&#xff0c;自动识别用户评论中的情感倾向。我们将使用真实的产品评论数据&#xff0c;从数据预处理到模型部署&#xff0c;完整展示神经网络在NLP领域…

now能减少mysql的压力吗

是否用数据库的 NOW() 能减少 MySQL 的压力&#xff1f;​答案是否定的——使用 NOW() 不仅不会降低压力&#xff0c;反而可能略微增加 MySQL 的负载。以下是详细分析&#xff1a;&#x1f50d; 性能对比&#xff1a;NOW() vs. Java 传参​指标​​Java 传参 (e.g., new Date()…

数据结构01:链表

数据结构 链表 链表和数组的区别 1. 存储方式 数组&#xff1a; 元素在内存中连续存储&#xff0c;占用一块连续的内存空间元素的地址可以通过索引计算&#xff08;基地址 索引 元素大小&#xff09;大小固定&#xff0c;在创建时需要指定容量 链表&#xff1a; 元素&#xf…

【Java学习|黑马笔记|Day21】IO流|缓冲流,转换流,序列化流,反序列化流,打印流,解压缩流,常用工具包相关用法及练习

标题【Java学习|黑马笔记|Day20】 今天看的是黑马程序员的《Java从入门到起飞》下部的95-118节&#xff0c;笔记包含IO流中的字节、字符缓冲流&#xff0c;转换流&#xff0c;序列化流反序列化流&#xff0c;打印流&#xff0c;解压缩流&#xff0c;常用工具包相关用法及练习 …

API网关原理与使用场景详解

一、API网关核心原理 1. 架构定位 #mermaid-svg-hpDCWfqoiLcVvTzq {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-hpDCWfqoiLcVvTzq .error-icon{fill:#552222;}#mermaid-svg-hpDCWfqoiLcVvTzq .error-text{fill:#5…

OSPF路由协议——上

OSPF路由协议 RIP的不足 以跳数评估的路由并非最优路径如果RTA选择s0/0传输&#xff0c;传输需时会大大缩短为3s 最大跳数为16跳&#xff0c;导致网络尺度小RIP协议限制网络直径不能超过16跳&#xff0c;并且16跳为不可达。 收敛速度慢 RIP 定期路由更新 更新计时器&#xff1a…

(LeetCode 面试经典 150 题) 219. 存在重复元素 II (哈希表)

题目&#xff1a;219. 存在重复元素 II 思路&#xff1a;哈希表&#xff0c;时间复杂度0(n)。 哈希表记录每个数最新的下标&#xff0c;遇到符合要求的返回true即可。 C版本&#xff1a; class Solution { public:bool containsNearbyDuplicate(vector<int>& nums,…

Cookies 详解及其与 Session 的协同工作

Cookies 详解及其与 Session 的协同工作 一、Cookies 的本质与作用 1. 什么是 Cookies&#xff1f; Cookies 是由服务器发送到用户浏览器并存储在本地的小型文本文件。核心特性&#xff1a; 存储位置&#xff1a;客户端浏览器数据形式&#xff1a;键值对字符串&#xff08;最大…

DeepSeek Janus Pro本地部署与调用

step1、Janus模型下载与项目部署 创建文件夹autodl-tmp https://github.com/deepseek-ai/Janus?tabreadme-ov-file# janusflow 查看是否安装了git&#xff0c;没有安装的话安装一下&#xff0c;或者是直接github上下载&#xff0c;上传到服务器&#xff0c;然后解压 git --v…

【Elasticsearch】BM25的discount_overlaps参数

discount_overlaps 是 Elasticsearch/Lucene 相似度模型&#xff08;Similarity&#xff09;里的一个布尔参数&#xff0c;用来决定&#xff1a;> 在计算文档长度归一化因子&#xff08;norm&#xff09;时&#xff0c;是否忽略“重叠 token”&#xff08;即位置增量 positi…

Linux | LVS--Linux虚拟服务器知识点(上)

一. 集群与分布式1.1 系统性能扩展方式当系统面临性能瓶颈时&#xff0c;通常有以下两种主流扩展思路&#xff1a;Scale Up&#xff08;向上扩展&#xff09;&#xff1a;通过增强单台服务器的硬件配置来提升性能&#xff0c;这种方式简单直接&#xff0c;但受限于硬件物理极限…

【Linux-云原生-笔记】keepalived相关

一、概念Keepalived 是一个用 C 语言编写的、轻量级的高可用性和负载均衡解决方案软件。 它的主要目标是在基于 Linux 的系统上提供简单而强大的故障转移功能&#xff0c;并可以结合 Linux Virtual Server 提供负载均衡。1、Keepalived 主要提供两大功能&#xff1a;高可用性&a…

计算机网络:概述层---计算机网络的组成和功能

&#x1f310; 计算机网络基础全景梳理&#xff1a;组成、功能与核心机制 &#x1f4c5; 更新时间&#xff1a;2025年7月21日 &#x1f3f7;️ 标签&#xff1a;计算机网络 | 网络组成 | 分布式 | 负载均衡 | 资源共享 | 网络可靠性 | 计网基础 文章目录前言一、组成1.从组成部…

Linux中scp命令传输文件到服务器报错

上传本地文件到Linux服务器使用scp命令报错解决办法使用scp命令报错 Could not resolve hostname e: Name or service not known 解决办法 不使用登录服务器的工具传输&#xff0c;打开本地cmd&#xff0c;使用scp命令传输即可。 scp E:\dcm-admin.jar root127.0.0.1:/

历史数据分析——国药现代

医药板块走势分析: 从月线级别来看 2008年11月到2021年2月,月线上走出了两个震荡中枢的月线级别2085-20349的上涨段; 2021年2月到2024年9月,月线上走出了20349-6702的下跌段; 目前月线级别放巨量,总体还在震荡区间内,后续还有震荡和上涨的概率。 从周线级别来看 从…