HarmonyOS 如何给tabbar添加export出的componet

HarmonyOS
2024-12-25 13:03:23
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

参考示例:

const COMMUNITY_TAB_BAR_INDEX: number = 2; // 初始化社区的tab下标
const ARC_MARGIN_TOP: number = -30; // 圆弧的上外间距为-30

export class TabBarDataType {
  id: number;
  defaultIcon: ResourceStr;

  constructor(id: number, defaultIcon: ResourceStr) {
    this.id = id;
    this.defaultIcon = defaultIcon;
  }
}

export const TABINFO: TabBarDataType[] = [
  new TabBarDataType(0, $r('app.media.startIcon')),
  new TabBarDataType(1, $r('app.media.startIcon')),
  new TabBarDataType(2, $r('app.media.startIcon')),
  new TabBarDataType(3, $r('app.media.startIcon')),
  new TabBarDataType(4, $r('app.media.startIcon')),
];

@Entry
@Component
struct TabViewDemo {
  @Provide selectedIndex: number = 0; // 初始化被选定的tabBar下标
  private controller: TabsController = new TabsController(); // 初始化Tab控制器

  build() {
    Column() {
      Tabs({ index: this.selectedIndex, barPosition: BarPosition.End, controller: this.controller }) {
        TabContent() {
          Text("tab1")
            .fontSize(26)
        }

        TabContent() {
          Text("tab2")
            .fontSize(26)
        }

        TabContent() {
          Text("tab3")
            .fontSize(26)
        }

        TabContent() {
          Text("tab4")
            .fontSize(26)
        }

        TabContent() {
          Text("tab5")
            .fontSize(26)
        }
      }
      .vertical(false)
      .scrollable(false)
      .layoutWeight(1)
      .backgroundColor('#ffdbd9d9')
      .barHeight(0)
      .onChange((index: number) => {
        this.selectedIndex = index;
      })

      // 自定义TabBar组件
      CustomTabBar({ selectedIndex: $selectedIndex })
    }.width("100%")
    .height("100%")
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
@Component
struct CustomTabBar {
  @Link selectedIndex: number; // 初始化被选定的tabBar下标

  build() {
    Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceAround, alignItems: ItemAlign.Center }) {
      ForEach(TABINFO, (item: TabBarDataType, tabIndex: number) => {
        // 单独一个TabBar组件
        TabItem({
          tabBarIndex: tabIndex,
          selectedIndex: $selectedIndex,
        })
      })
    }
    .height(60)
  }
}

@Component
struct TabItem {
  @Prop tabBarIndex: number; // tabBar下标
  @Link selectedIndex: number; // 初始化被选定的tabBar下标

  build() {
    Column() {
      // 判断tab的下标是否为2
      if (this.tabBarIndex === COMMUNITY_TAB_BAR_INDEX) {
        Column() {
          Image(TABINFO[this.tabBarIndex].defaultIcon)
            .size({ width: 43, height: 43 })
            .interpolation(ImageInterpolation.High)
            .borderRadius(22)
        }
        .width(52)
        .height(52)
        .borderRadius(24)
        .margin({ top: ARC_MARGIN_TOP })
        .backgroundColor(Color.White)
        .justifyContent(FlexAlign.Center)
      } else {
        Column() {
          // 通过被选中的tabBar下标值和tabBar的默认下标值来改变图片显示
          Image(TABINFO[this.tabBarIndex].defaultIcon)
            .interpolation(ImageInterpolation.High)
            .size(
              { width: 28, height: 28 })
            .borderRadius(14)
        }
        .width(37)
        .height(37)
        .justifyContent(FlexAlign.Center)
      }
    }
    .width(60)
    .onClick(() => {
      // 更新被选中的tabBar下标
      this.selectedIndex = this.tabBarIndex;
    })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
分享
微博
QQ
微信
回复
2024-12-25 15:24:59
相关问题
如何按钮添加图片?
6603浏览 • 4回复 待解决
如何在Tabs中tabBar添加其他组件
1506浏览 • 1回复 待解决
HarmonyOS 如何 app 添加水印
1206浏览 • 1回复 待解决
如何组件添加双击事件?
1156浏览 • 1回复 待解决
exportexport default区别
3251浏览 • 1回复 待解决
如何文字添加下划线?
1410浏览 • 1回复 待解决
文本添加上划线如何实现?
959浏览 • 1回复 待解决
HarmonyOS 怎么web组件请求添加header
1202浏览 • 1回复 待解决
HarmonyOS @Export与@Style导出问题
1307浏览 • 1回复 待解决
HarmonyOS export struct初始化参数
1014浏览 • 1回复 待解决