鸿蒙tabbar ArkTS

鸿蒙tabbar ArkTS

做了仿照现在应用的做了一个tabbar。

官方文档地址
参考文档

tabbar

其中有个比较重要的点是,对image资源的引用问题。

资源相关说明

图片是resources目录下的base目录下的。

media目录下的图片的资源不能添加文件夹,只能是文件,而且文件的命名规则是只能包含字母大小写和下划线。

另外{资源引用的方式](https://developer.harmonyos.com/cn/docs/documentation/doc-references-V3/ts-types-0000001477981241-V3)

要求资源定义的内容必须在编码阶段确定,中间不能更改,因此不能增加if之类的判断,只能把整个资源引用当做参数传递进去。

实现代码如下:

javascript 复制代码
@Entry
@Component
struct Index {
  @State message: string = 'Hello World'
  @State selectedIndex: number = 0

  @Builder TabBuider(index: number, name: string, selectedImage: Resource, normalImage: Resource) {
    Column() {
      if ( this.selectedIndex == index) {
        Image(selectedImage)
          .objectFit(ImageFit.Contain)
          .width(24)
          .height(24)
          .margin({bottom: '4lpx'})
      } else {
        Image(normalImage)
          .objectFit(ImageFit.Contain)
          .width(24)
          .height(24)
          .margin({bottom: '4lpx'})
      }

      Text(name)
        .fontSize(16)
    }
    .border({
      width: {top: '2lpx'},
      color: '#efefef',
      style: BorderStyle.Solid
    })
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }

  build() {
    Tabs({barPosition: BarPosition.End}) {
      TabContent() {
        Text(this.message)
      }
      .tabBar(this.TabBuider(0, "首页", $r('app.media.tabbar_home_selected'), $r('app.media.tabbar_home')))

      TabContent() {
        Text(this.message)
      }
      .tabBar(this.TabBuider(1, "数据", $r('app.media.tabbar_data_selected'), $r('app.media.tabbar_data')))

      TabContent() {
        Text(this.message)
      }
      .tabBar(this.TabBuider(2, "我的", $r('app.media.tabbar_mine_selected'), $r('app.media.tabbar_mine')))
    }.onChange(index=>{
      this.selectedIndex = index
    })
  }
}
相关推荐
以太浮标9 分钟前
华为eNSP模拟器综合实验之- DHCP Option 43 解析
服务器·网络·华为·云计算
小哥Mark12 分钟前
Flutter开发鸿蒙年味 + 实用实战应用|春节祝福:列表选卡 + 贴纸拖动 + 截图分享
flutter·harmonyos·鸿蒙
m0_6855350827 分钟前
华为光学工程师面试题汇总
华为·光学·光学设计·光学工程·镜头设计
王码码203530 分钟前
Flutter for OpenHarmony 实战之基础组件:第十六篇 约束布局 ConstrainedBox 与 AspectRatio
flutter·harmonyos
2501_9219308334 分钟前
基础入门 React Native 鸿蒙跨平台开发:Video 全屏播放与画中画 鸿蒙实战
react native·react.js·harmonyos
果粒蹬i38 分钟前
【HarmonyOS】鸿蒙React Native 实战:打造流畅的底部导航
react native·华为·harmonyos
2501_9219308340 分钟前
基础入门 React Native 鸿蒙跨平台开发:react-native-switch 开关适配
react native·react.js·harmonyos
王码码20351 小时前
Flutter for OpenHarmony 实战之基础组件:第十八篇 布局终极者 CustomScrollView 与 Slivers
flutter·harmonyos
一起养小猫1 小时前
Flutter for OpenHarmony 实战:打地鼠游戏完整开发指南
flutter·游戏·harmonyos
一起养小猫1 小时前
Flutter for OpenHarmony 实战:打地鼠游戏难度设计与平衡性
flutter·游戏·harmonyos