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

鸿蒙仓颉开发语言实战教程:自定义tabbar

大家周末好呀,今天继续分享仓颉语言开发商城应用的实战教程,今天要做的是tabbar。

大家都知道ArkTs有Tabs和TabContent容器,能够实现上图的样式,满足基本的使用需求。而仓颉就不同了,它虽然也有这两个组件,但是它的tabbar参数只支持传入图片或者文字,不能像ArkTs那样能传入组件,所以在仓颉语言中官方的tabbar局限性非常大。

给大家实操讲解一下,下面是一段Tabs的基本写法:

Tabs(BarPosition.End, this.controller){TabContent(){Text('页面1')}TabContent(){Text('页面2’)}
}

如果你要设置tabbar的样式,需要在TabContent下添加tabbar属性,然后你会发现tabbar只有唯二的两个参数:

TabContent(){Text('页面1')}.tabBar(icon: CJResource, text: CJResource)

设置完之后它长这样:

这样就无法满足我们的需求,所以我们需要自定义。

每一个tabbar元素都有一个图片组件和一个文字组件,我给它写出来:

Column {Image(item.selectIcon).width(28).height(28)Text(item.title).fontSize(15).fontColor(0xd84642).margin(top: 3)}

然后它需要有一个选中状态,难受的是仓颉不支持三元表达式,所以我只能写if语句:

Column {if(this.currenttabIndex == index){Image(item.selectIcon).width(28).height(28)Text(item.title).fontSize(15).fontColor(0xd84642).margin(top: 3)}else {Image(item.icon).width(28).height(28)Text(item.title).fontSize(15).fontColor(Color.GRAY).margin(top: 3)}}

它还需要一个点击事件:

Column {if(this.currenttabIndex == index){Image(item.selectIcon).width(28).height(28)Text(item.title).fontSize(15).fontColor(0xd84642).margin(top: 3)}else {Image(item.icon).width(28).height(28)Text(item.title).fontSize(15).fontColor(Color.GRAY).margin(top: 3)}}
.onClick({evet => this.currenttabIndex = index;this.controller.changeIndex(Int32(this.currenttabIndex))})

这样一个元素就写好了,接下来我只要循环添加几个元素,一个完整的tabbar就写好了,这里大家也要注意一下仓颉中foreach的写法:

Row {ForEach(this.tabList, itemGeneratorFunc: {item: TabItem, index: Int64 =>Column {if(this.currenttabIndex == index){Image(item.selectIcon).width(28).height(28)Text(item.title).fontSize(15).fontColor(0xd84642).margin(top: 3)}else {Image(item.icon).width(28).height(28)Text(item.title).fontSize(15).fontColor(Color.GRAY).margin(top: 3)}}.onClick({evet => this.currenttabIndex = index;this.controller.changeIndex(Int32(this.currenttabIndex))})})
}
.width(100.percent)
.height(60)
.alignItems(VerticalAlign.Center)
.justifyContent(FlexAlign.SpaceAround)

最后我们还是需要官方的Tabs容器来添加页面,你只要不设置tabbar属性底部导航栏区域就是空白的,正好把我们自定义的tabbar放上,下面是完整的示例代码:

let tabList: Array<TabItem> = [TabItem(@r(app.media.shop_tab_00), @r(app.media.shop_tab_01), '首页'),TabItem(@r(app.media.shop_tab_10), @r(app.media.shop_tab_11), '购物车'),TabItem(@r(app.media.shop_tab_20), @r(app.media.shop_tab_21), '我的')]
@State
var currenttabIndex:Int64 = 0Stack(Alignment.Bottom) {Tabs(BarPosition.End, this.controller){TabContent(){home()}TabContent(){shopcar()}TabContent(){mine()}}.barHeight(60).scrollable(false).animationDuration(0)Row {ForEach(this.tabList, itemGeneratorFunc: {item: TabItem, index: Int64 =>Column {if(this.currenttabIndex == index){Image(item.selectIcon).width(28).height(28)Text(item.title).fontSize(15).fontColor(0xd84642).margin(top: 3)}else {Image(item.icon).width(28).height(28)Text(item.title).fontSize(15).fontColor(Color.GRAY).margin(top: 3)}}.onClick({evet => this.currenttabIndex = index;this.controller.changeIndex(Int32(this.currenttabIndex))})})
}
.width(100.percent)
.height(60)
.alignItems(VerticalAlign.Center)
.justifyContent(FlexAlign.SpaceAround)
}

以上就是仓颉语言自定义tabbar的实现过程,感谢阅读。#HarmonyOS语言##仓颉##购物#

相关文章:

  • 常州做网站的 武进软件开发培训机构去哪个学校
  • 大连电子学校网站建设想要网站推广页
  • 网站seo招聘自己建站的网站
  • 建设一个能看视频的网站品牌运营推广方案
  • 做网站哪家比较好安徽建站
  • 网站改版应该怎么做我的百度账号
  • centos7.9使用docker-compose安装kafka
  • GitAny - 無需登入的 GitHub 最新倉庫檢索工具
  • 图像分割技术的实现与比较分析
  • RabbitMQ 应用 - SpringBoot
  • html使用JS实现账号密码登录的简单案例
  • MFC: 文件加解密(单元测试模块)
  • 理解局部放电分析中的 PRPD 和 PRPS 图
  • 在Spring Boot中实现Kafka动态反序列化:针对多主题的灵活数据处理
  • MySQL数据库零基础入门教程:从安装配置到数据查询全掌握【MySQL系列】
  • 【图像大模型】Stable Diffusion XL:下一代文本到图像生成模型的技术突破与实践指南
  • 用python写节奏大师小游戏
  • 基于大模型的急性腐蚀性胃炎风险预测与诊疗方案研究报告
  • QT5.15 MacOS 打包指南
  • VLLM框架部署Qwen大模型,应该选择哪个qwen3系列的大模型和什么硬件配置?
  • Windows系统如何查看ssh公钥
  • pcdn的发展
  • 论文阅读:2024 arxiv Prompt Injection attack against LLM-integrated Applications
  • java-单列集合list与set。
  • 【SpringBoot】从零开始全面解析Spring IocDI (二)
  • 学习threejs,使用three-spritetext实现黑客帝国数字雨效果