我通过导航栏的例子简单演示了一下通过官方的示例代码快速进行一些功能的开发,希望对您有用,如果你有别的想法什么的,也可以在评论区留下您的发言

以下是简单介绍:

我们打开华为开发者联盟官网,然后在顶部的导航栏里面找到开发,在导航栏下方的几个图标里面找到示例代码,点进去并找到我视频里面那个一多导航栏,下载下来

直接前往gitee下载
打开topTabView. ets文件,找到List,观察代码可以发现这是一个判断是否选中的代码,我们可以去查找官网最佳实践的代码,并对它进行一些修改,修改如下:

//.../view/TopTabView.ets
ListItem() {
            Column(){
              Text(item)
                .fontWeight(index === this.secondLevelIndex ? 700 : 500)
                .fontColor(index === this.secondLevelIndex ? Color.Blue : '#99000000') //更改颜色判断
                .width(index === this.secondLevelIndex ? 65 : 50)
                .textAlign(TextAlign.Center)
                .lineHeight(index === this.secondLevelIndex ? 33 : 25)
                .borderRadius(4)
                .maxLines(1)
                .textOverflow({ overflow: TextOverflow.Ellipsis })
              Divider() //添加下划线
                .width(48)
                .strokeWidth(1)
                .color('#0A59F7')
                .opacity(index === this.secondLevelIndex ? 1 : 0) //下划线显隐判断
            }
          }


我们移步主页代码(home.ets)找到Tab组件,并根据API文档,对它进行叠加和模糊的设置,代码如下:

//.../view/Home.ets
      .barOverlap(true) //添加层叠与模糊属性
      .barWidth(this.currentWidthBreakpoint === 'lg' ? 96 : '100%')
      .barHeight(this.currentWidthBreakpoint === 'lg' ? '100%' : 76)
      .barMode(this.currentWidthBreakpoint === 'lg' ? BarMode.Scrollable : BarMode.Fixed,
        { nonScrollableLayoutStyle: LayoutStyle.ALWAYS_CENTER })
      .vertical(this.currentWidthBreakpoint === 'lg')
      .barBackgroundBlurStyle(BlurStyle.COMPONENT_THICK)
      .onChange((index: number) => {
        this.firstLevelIndex = index;
      })


经过简单的修改,这个页面已经被我们自定义好了,剩余的组件我们可以继续开发,如果加了一个关于页面,代码如下:

//.../view/About.ets 自己新建的文件
@Entry
@Component
export struct About {
  build(){
    Column(){
      Image($r('app.media.about'))
        .width(60)
        .borderRadius(30)
        .margin(10)
      Text('User')
        .margin({bottom : 300})
      Image($r('app.media.startIcon'))
        .width(20)
        .margin({bottom : 10})
      Text('About the Sample')
      Text('Version 1.0.0')
    }.width('100%')
    .margin({top : 50})
  }
}

//.../view/Home.ets
Tabs({
        barPosition: this.currentWidthBreakpoint === 'lg' ? BarPosition.Start : BarPosition.End
      }) {
        TabContent() {
          TopTabView({
            firstLevelIndex: this.firstLevelIndex,
            tabData: this.tabData
          })
        }
        .tabBar(this.tabBuilder(this.tabData.getFirstList()[0], 0))

        TabContent()
          .tabBar(this.tabBuilder(this.tabData.getFirstList()[1], 1))

        TabContent()
          .tabBar(this.tabBuilder(this.tabData.getFirstList()[2], 2))

        TabContent(){
          About() //加入关于页面,运行后手机页面可出现关于页面
        }
          .tabBar(this.tabBuilder(this.tabData.getFirstList()[3], 3))
      }


这就是本视频的所有内容,我们对官网的sample进行了简单的修改,并达到了我们自己想要的效果,这也证明官网的sample和最佳实践主义大大节省我们的时间,提高我们开发的效率

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐