鸿蒙~ArtUI基础开发-Navigation组件

2023-12-20  本文已影响0人  胡修波

页面模式设置

书城 demo

import { TabBars } from './TabBars'

const TAG = "huxiubo";


@Entry
@Component
struct Index {

  menusCallBack1() {
    console.info(TAG, `call menusCallBack1`);
  }

  menusCallBack2() {
    console.info(TAG, `call menusCallBack2`);
  }

  menusCallBack3() {
    console.info(TAG, `call menusCallBack3`);
  }

  toolBarCallBack1() {
    console.info(TAG, `call toolBarCallBack1`);
  }

  toolBarCallBack2() {
    console.info(TAG, `call toolBarCallBack2`);
  }

  toolBarCallBack3() {
    console.info(TAG, `call toolBarCallBack3`);
  }

  build() {
    Row() {
      Navigation() {
        Column() {
          TabBars()
        }
      }
      .size({width:"100%", height:"100%"})
      .title("掌阅书城")
      .titleMode(NavigationTitleMode.Free)
      .menus([
        {value: "",icon: "../resources/base/media/icon.png",  action: this.menusCallBack1},
        {value: "",icon: "../resources/base/media/icon_read_setting_space_default.png", action: this.menusCallBack2},
        {value: "",icon: "../resources/base/media/icon_read_style_more.png", action: this.menusCallBack3},
      ])
      .toolBar({items: [
        {value: "func", icon: "../resources/base/media/icon.png", action: this.toolBarCallBack1},
        {value: "func", icon: "../resources/base/media/icon.png", action: this.toolBarCallBack2},
        {value: "func", icon: "../resources/base/media/icon.png", action: this.toolBarCallBack3}
      ]})
    }
    .backgroundColor('#f1f3f5')
  }
}

TabBars

@Component
export struct TabBars {
  @State tabsIndex: number = 0;
  @State initBarData : string[] = ["精选", "漫画", "频道", "频道1","得到"]

  @Builder
  tabBarBuilder (content:string, index: number) {
     Column() {
       Text(content)
         .fontSize(this.tabsIndex === index ? 20: 16)
         .fontColor(this.tabsIndex === index ? "#000" : "#666")
     }
  }

  build() {
    Tabs() {
      ForEach(this.initBarData, (item: string, index:number) => {
          TabContent() {
            Column() {
              Text(item)
                .fontSize(80)
            }
            .justifyContent(FlexAlign.Center)
            .width("100%")
            .height("100%")
          }
          .tabBar(this.tabBarBuilder(item, index !== undefined? index:0 ))
      })
    }
    .onChange((index: number) => {
      this.tabsIndex = index
    })
  }
}
微信图片_20231220190811.png
上一篇 下一篇

猜你喜欢

热点阅读