#放码来战.端云一体化开发#HarmonyOS 5 【农民叔叔】12.【端侧工程】Tabs容器组件页签导航栏

使用Tabs容器组件实现底部导航栏,切换内容页签。如下所示:

一、创建一个容器页面MainPage.ets

二、自定义tabBar组件

组件输入参数包括:导航按钮标题、选项索引、默认未选择图标、被选择图标。

@Builder
  TabBuilder(title:string,index:number,selectedImg:Resource,normalImg:Resource){
    Column(){
      Image(this.currentIndex===index?selectedImg:normalImg)
        .width(25)
        .height(25)
      Text(title)
        .margin({top:4})
        .fontSize(10)
        .fontColor(this.currentIndex=== index?'#1698CE':'#6B6B6B')
    }
    .justifyContent( FlexAlign.Center)
    .height('100%')
    .width('100%')
  }

三、TabContent的tabBar属性使用我们自定义的TabBuilder

TabContent() {
        // TODO:添加内容页
      }
      .tabBar(
        this.TabBuilder('首页', 0,
          $r('app.media.icon_home_selected'),
          $r('app.media.icon_home_normal')))

四、新建2个页面,“首页”HomePage和“我的”MinePage,作为内容页添加到TabContent(){}。

MainPage.ets文件的完整代码如下:

import HomePage from './HomePage';
import MinePage from './MinePage';

@Entry
@Component
struct MainPage {
  @State currentIndex: number = 0; // 当前选中的tab索引,默认首页

  build() {
    Tabs({ barPosition: BarPosition.End, index: 0 }) {
      TabContent() {
        // TODO:添加内容页
      }
      .tabBar(
        this.TabBuilder('首页', 0,
          $r('app.media.icon_home_selected'),
          $r('app.media.icon_home_normal')))

      TabContent() {
        // 我的
        MinePage()
      }
      .tabBar(this.TabBuilder('我的', 1, $r('app.media.icon_mine_selected'), $r('app.media.icon_mine_normal')))
    }
    .vertical(false)
    .backgroundColor(Color.White)
    .barMode(BarMode.Fixed)
    .scrollable(false)
    .onChange((index: number) => {
      this.currentIndex = index;
    })
  }

  @Builder
  TabBuilder(title: string, index: number, selectedImg: Resource, normalImg: Resource) {
    Column() {
      Image(this.currentIndex === index ? selectedImg : normalImg)
        .width(25)
        .height(25)
      Text(title)
        .margin({ top: 4 })
        .fontSize(10)
        .fontColor(this.currentIndex === index ? '#1698CE' : '#6B6B6B')
    }
    .justifyContent(FlexAlign.Center)
    .height('100%')
    .width('100%')
  }
}

更多关于Tabs容器组件的使用,请参见官方文档:

文档中心

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鸿花粉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值