鸿蒙的底部菜单导航实现

开始入坑鸿蒙

效果图图下:

Index代码如下:

import Home from "../pages/home/Home" //首页
import Classify from "./classify/Classify" //分类
import Mine from "../pages/mine/Mine" //我的
@Entry
@Component
struct Index {
  @State currentIndex: number = 0
  private controller: TabsController = new TabsController()
  // 只有被@Entry装饰的组件才可以调用页面的生命周期
  onPageShow() {
    console.info('Index onPageShow');
  }
  // 只有被@Entry装饰的组件才可以调用页面的生命周期
  onPageHide() {
    console.info('Index onPageHide');
  }
  // 只有被@Entry装饰的组件才可以调用页面的生命周期
  onBackPress() {
    console.info('Index onBackPress');
  }
  build() {
    Column() {
      Tabs({
        barPosition: BarPosition.End,
        controller: this.controller
      }) {
        TabContent() {
          Home()
        }.tabBar(this.TabBuilder('首页', 0, $r('app.media.home_yes'), $r('app.media.home_no')))
        TabContent() {
          Classify()
        }.tabBar(this.TabBuilder('分类', 1, $r('app.media.classify_yes'), $r('app.media.classify_no')))

        TabContent() {
          Mine()
        }.tabBar(this.TabBuilder('我的', 2, $r('app.media.mine_yes'), $r('app.media.mine_no')))
      }.scrollable(false) // 禁止滑动切换
    }
    .width('100%')
    .height('100%')
  }
  // 自定义导航页签的样式
  @Builder TabBuilder(title: string, targetIndex: number, selectedImg: Resource, normalImg: Resource) {
    Column() {
      Divider()
        .color('#e0e0e0') // 分割线颜色
        .height(1) // 分割线高度
        .margin({ top: 5 }) // 分割线上方的间距
      Image(this.currentIndex === targetIndex ? selectedImg : normalImg)
        .margin({ top: 5 })
        .size({ width: 25, height: 25 })
      Text(title)
        .fontColor(this.currentIndex === targetIndex ? '#1296db' : '#8a8a8a')
        .fontWeight(this.currentIndex === targetIndex ? FontWeight.Bold : FontWeight.Normal)

    }
    .width('100%')
    .height(50)
    .justifyContent(FlexAlign.Center)
    .onClick(() => {
      this.currentIndex = targetIndex
      this.controller.changeIndex(this.currentIndex)
    })
  }

}

单个页面(其他都一样)

@Component
export default struct Home {
  pageStack: NavPathStack = new NavPathStack()
  build() {
    Column() {
      Text("首页").width('100%').height('100%').textAlign(TextAlign.Center).fontSize("25fp")
    }.size({ width: '100%', height: '100%' })
  }
}

项目目录截图

我这里也给出工程代码:https://download.csdn.net/download/qq_41733851/89930328?spm=1001.2014.3001.5501

### 鸿蒙 Navigation 组件实现底部工具栏页面切换 在鸿蒙系统中,`Navigation` 组建能够通过 `toolbarConfiguration` 属性配置工具栏的位置和样式[^2]。为了实现底部工具栏上进行页面间的切换,开发者可以利用 `Navigation` 的这一特性。 下面是一个简单的示例代码片段展示如何创建带有底部工具栏的多页应用,并允许用户点击不同的按钮来切换当前显示的内容: ```java // 创建一个新的 Navigation 对象并指定其工作模式为自动调整布局方式 Navigation nav = new Navigation(context).mode(NavigationMode.Auto); // 定义多个子页面的信息列表,这里假设我们有三个主要视图 List<NavDestination> destinations = Arrays.asList( createPageInfo("Home", R.drawable.ic_home, HomeFragment.class), createPageInfo("Messages", R.drawable.ic_message, MessagesFragment.class), createPageInfo("Profile", R.drawable.ic_profile, ProfileFragment.class) ); // 设置初始选中的页面索引,默认打开首页 int selectedIndex = 0; // 构建 ToolbarConfig 来定制化底部导航条的行为与外观 ToolbarConfig toolbarConfig = new ToolbarConfig() .position(ToolbarPosition.BOTTOM) // 将工具栏放置于屏幕下方 .items(destinations.stream().map(item -> item.iconResId).collect(Collectors.toList())) .onItemSelected((index) -> { // 当选择了某个项目时更新所呈现的内容区域 showContentForIndex(index); selectedIndex = index; }); nav.toolbarConfiguration(toolbarConfig); // 应用已定义好的配置到 Navigation 上 private void showContentForIndex(int index){ Fragment fragmentToShow = null; switch (index){ case 0: fragmentToShow = new HomeFragment(); break; case 1: fragmentToShow = new MessagesFragment(); break; case 2: fragmentToShow = new ProfileFragment(); break; } getSupportFragmentManager() .beginTransaction() .replace(R.id.content_container, fragmentToShow) .commit(); } private NavDestination createPageInfo(String title, int iconResourceId, Class<? extends Fragment> fragmentClass){ return new NavDestination(title, iconResourceId, fragmentClass); } ``` 此段代码展示了如何初始化一个 `Navigation` 实例,并为其指定了一个包含三个项目的菜单项集合;这些项目代表应用程序的不同部分(主页、消息和个人资料)。接着设置了底部位置上的工具栏以及相应的事件监听器以便处理用户的交互操作。最后提供了辅助函数用于动态加载对应的碎片(Fragment),从而实现了基于选择变化而改变内容区的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

来之梦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值