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

【鸿蒙学习笔记】构建布局・选项卡 (Tabs)

官方文档:选项卡 (Tabs)

目录标题

  • 底部导航
  • 顶部导航
  • 侧边导航
  • 限制导航栏的滑动切换
  • 固定导航栏・可滚动导航栏
  • 自定义导航栏
    • 切换至指定页签

底部导航

@Entry
@Component
struct Bujv_tabs {build() {Column() {Tabs({ barPosition: BarPosition.End }) {TabContent() {Text('首页的内容').fontSize(30)}.tabBar('首页')TabContent() {Text('推荐的内容').fontSize(30)}.tabBar('推荐')TabContent() {Text('发现的内容').fontSize(30)}.tabBar('发现')TabContent() {Text('我的内容').fontSize(30)}.tabBar("我的")}}.width('100%')}
}

顶部导航

Tabs({ barPosition: BarPosition.Start })

侧边导航

Tabs({ barPosition: BarPosition.Start }) {}
.vertical(true)
.barWidth(100)
.barHeight('100%')

限制导航栏的滑动切换

.scrollable(false)

固定导航栏・可滚动导航栏

.barMode(BarMode.Fixed) // 固定导航栏
.barMode(BarMode.Scrollable) // 可滚动导航栏

自定义导航栏

@Entry
@Component
struct Bujv_tabs {@State currentIndex: number = 0@BuildertabBuilder(title: string, targetIndex: number, selectedImg: Resource, normalImg: Resource) {Column() {Image(this.currentIndex === targetIndex ? selectedImg : normalImg).size({ width: 25, height: 25 })Text(title).fontColor(this.currentIndex === targetIndex ? Color.Red : Color.Black)}.width('100%').height(50).justifyContent(FlexAlign.Center)}build() {Column() {Tabs({ barPosition: BarPosition.Start }) {TabContent() {Column() {Text('我的内容')}.width('100%').height('100%').backgroundColor(Color.Pink)}.tabBar(this.tabBuilder('我的', 0, $r('app.media.fuel'), $r('app.media.foods')))}}.width('100%')}
}

切换至指定页签

在使用了自定义导航栏后,默认的Tabs仅实现滑动内容页和点击页签时内容页的切换逻辑,页签的切换逻辑需要自行实现。

@Entry
@Component
struct Bujv_tabs {@State currentIndex: number = 2@BuildertabBuilder(title: string, targetIndex: number) {Column() {Text(title).fontColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B')}}build() {Column() {Tabs({ barPosition: BarPosition.End }) {TabContent() {Text('首页的内容').fontSize(30)}.tabBar(this.tabBuilder('首页', 0))TabContent() {Text('推荐的内容').fontSize(30)}.tabBar(this.tabBuilder('推荐', 1))TabContent() {Text('发现的内容').fontSize(30)}.tabBar(this.tabBuilder('发现', 2))TabContent() {Text('我的内容').fontSize(30)}.tabBar(this.tabBuilder("我的", 3))}.animationDuration(2).backgroundColor('#F1F3F5').onChange((index: number) => {this.currentIndex = index})}.width('100%')}
}

TabsController是Tabs组件的控制器,用于控制Tabs组件进行内容页切换。

@Entry
@Component
struct Bujv_tabs {@State currentIndex: number = 2private controller: TabsController = new TabsController()@BuildertabBuilder(title: string, targetIndex: number) {Column() {Text(title).fontColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B')}}build() {Column() {Tabs({ barPosition: BarPosition.End, index: this.currentIndex, controller: this.controller }) {TabContent() {Text('首页的内容').fontSize(30)}.tabBar(this.tabBuilder('首页', 0))TabContent() {Text('推荐的内容').fontSize(30)}.tabBar(this.tabBuilder('推荐', 1))TabContent() {Text('发现的内容').fontSize(30)}.tabBar(this.tabBuilder('发现', 2))TabContent() {Text('我的内容').fontSize(30)}.tabBar(this.tabBuilder("我的", 3))}.animationDuration(2).backgroundColor('#F1F3F5').height(600).onChange((index: number) => {this.currentIndex = index})Button('动态修改index').width('50%').margin({ top: 20 }).onClick(() => {this.currentIndex = (this.currentIndex + 1) % 4})Button('changeIndex').width('50%').margin({ top: 20 }).onClick(() => {let index = (this.currentIndex + 1) % 4this.controller.changeIndex(index)})}.width('100%')}
}
http://www.lryc.cn/news/402862.html

相关文章:

  • 独立游戏《星尘异变》UE5 C++程序开发日志5——实现物流系统
  • Web开发:<br>标签的作用
  • DVC+Minio
  • C++内存管理(区别C语言)深度对比
  • 手把手带你写一个精简版 HashMap 的 put 方法
  • 【面试题】数据结构:堆排序的排序思想?
  • PyTorch 深度学习实践-循环神经网络基础篇
  • vue实现可拖拽dialog封装
  • 本地多模态看图说话-llava
  • 人工智能算法工程师(中级)课程14-神经网络的优化与设计之拟合问题及优化与代码详解
  • Java异常抛出与处理方法
  • 兼容性测试主要有什么类型?
  • 设计模式--组合模式
  • ArduPilot开源代码之AP_DAL_RangeFinder
  • SpringCloud教程 | 第九篇: 使用API Gateway
  • 数据结构——hash(hashmap源码探究)
  • 国产麒麟、UOS在线打开pdf加盖印章
  • 破解反爬虫策略 /_guard/auto.js(二)实战
  • 同样是人工智能 客户在哪儿AI和GPT等大模型有什么不同
  • AES Android IOS H5 加密方案
  • 一文了解变阻器和电位器的定义、原理、应用及其对比
  • WPF实现一个带旋转动画的菜单栏
  • 使用Dockerfile构建镜像
  • 概率论原理精解【3】
  • [C/C++入门][循环]14、计算2的幂(2的n次方)
  • RPC与服务的注册发现
  • 3112. 访问消失节点的最少时间 Medium
  • FastAPI 学习之路(五十二)WebSockets(八)接受/发送json格式消息
  • Go语言并发编程-案例_3
  • pikachu之跨站脚本攻击(x‘s‘s)