#夏日挑战赛# 【木棉花】:ETS自定义导航栏组件 原创 精华

木棉花潘颖琳
发布于 2022-7-15 00:43
浏览
4收藏

前言

「本文正在参加星光计划3.0–夏日挑战赛」
这次给大家带来的是ets自定义导航栏组件,以仿淘宝的导航栏为案例Demo来讲解。

概述

效果图如下:

#夏日挑战赛# 【木棉花】:ETS自定义导航栏组件-鸿蒙开发者社区

当被选中时,字体样式和图片都会相应变化。

正文

新建空项目

SDK选择7以上,language选择ets

#夏日挑战赛# 【木棉花】:ETS自定义导航栏组件-鸿蒙开发者社区
#夏日挑战赛# 【木棉花】:ETS自定义导航栏组件-鸿蒙开发者社区

初始化导航栏

在media文件夹下存放所需的图片,初始设置导航栏选中的页签index为0,同时定义一个静态数组存放导航菜单的数据——标题名称、选中与未选中状态的图片。案例Demo代码如下:

  @State selectedIndex: number = 0
  @State menuData: any[]= [{
                             "text": " 首页 ",
                             "inActiveImg": $r('app.media.shouye'),
                             "activeImg": $r('app.media.taobao')
                           },
                           {
                             "text": " 逛逛 ",
                             "inActiveImg": $r('app.media.guangguang'),
                             "activeImg": $r('app.media.guangguang_on'),
                           },
                           {
                             "text": " 消息 ",
                             "inActiveImg": $r('app.media.msg'),
                             "activeImg": $r('app.media.msg_on'),
                           },
                           {
                             "text": "购物车",
                             "inActiveImg": $r('app.media.gouwuche'),
                             "activeImg": $r('app.media.gouwuche_on'),
                           },
                           {
                             "text": "我的淘宝",
                             "inActiveImg": $r('app.media.wode'),
                             "activeImg": $r('app.media.wode_on'),
                           },
  ]

布局样式

单个页签由图片和文本组成,为纵向布局;整体导航栏为水平布局;利用ForEach循环遍历导航菜单栏的数据,并通过判断是否选中来确定所显示的图片及文本样式(边框可根据喜好添加),案例代码如下:

  build() {
    Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceAround }) {
      ForEach(this.menuData.map((item1, index1) => {
        return { i: index1, data: item1 };
      }),
        item => { // Parameter 2: item generator
            Column({ space: 5 }) {
              Image(this.selectedIndex === item.i ? item.data.activeImg : item.data.inActiveImg
              ).width(30).height(30).objectFit(ImageFit.Cover)

              Text(item.data.text)
                .fontSize(15)
                .fontWeight(FontWeight.Bold)
                .fontColor(this.selectedIndex === item.i ? '#FC7D0C' : '#bfbfbf')

            }.height('60').onClick(() => {
              this.selectedIndex = item.i;
            })
        }, item => item.i.toString()
      )
    }
    .borderColor(Color.Gray)
    .borderWidth(0.2)
    .width('100%')
    .height('9%')
  }

到此步,可预览效果如下:

#夏日挑战赛# 【木棉花】:ETS自定义导航栏组件-鸿蒙开发者社区

封装使用

1、可以新建一个etsPage,将MyTab组件设置为export,然后在新页面import来使用;

@State selectedIndex: number = 0

改为

  @Link selectedIndex: number

这是为了双向数据绑定
案例Demo新建的页面Test.ets,代码如下:

import { MyTab } from './MyTab'

@Entry
@Component
struct Test {
  @State selectedIndex: number = 0

  build() {
    Flex({ direction: FlexDirection.Column }) {
      Flex({ direction: FlexDirection.Column }) {
        if (this.selectedIndex == 0) {
          Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
            Text('这是首页的界面').fontSize(30)
          }.height('92%').width('98%')
        } else if (this.selectedIndex == 1) {
          Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
            Text('这是逛逛的界面').fontSize(30)
          }.height('92%').width('98%')
        } else if (this.selectedIndex == 2) {
          Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
            Text('这是消息界面').fontSize(30)
          }.height('92%').width('98%')
        } else if (this.selectedIndex == 3) {
          Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
            Text('这是购物车的界面').fontSize(30)
          }.height('92%').width('98%')
        } else if (this.selectedIndex == 4) {
          Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
            Text('这是我的界面').fontSize(30)
          }.height('92%').width('98%')
        }
      }
      .width('98%')
      .height('91%')

      MyTab({ selectedIndex: $selectedIndex })
    }
    .width('100%')
    .height('100%')
  }
}

2、也可以在同一Page将其作为子组件来使用

作为子组件来使用的话就直接将MyTab移至同一ets文件,无需添加export和import模块,不过同样需要将选中的索引selectedindex类型改为双向数据绑定@Link

结语

以上就是我这次的小分享啦❀❀!欢迎评论区交流哦O(∩_∩)O

更多资料请关注我们的项目 : Awesome-Harmony_木棉花

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
MyTab_ets.zip 3.48M 40次下载
已于2022-7-15 00:43:01修改
5
收藏 4
回复
举报
3条回复
按时间正序
/
按时间倒序
红叶亦知秋
红叶亦知秋

赞,接下来可以实现各个页面的功能了。

回复
2022-7-15 10:41:07
木棉花潘颖琳
木棉花潘颖琳 回复了 红叶亦知秋
赞,接下来可以实现各个页面的功能了。

感谢支持❀

回复
2022-7-15 14:46:53
Buty9147
Buty9147

代码很简洁!

回复
2022-7-15 17:59:54
回复
    相关推荐