【从零开始鸿蒙开发:01】自定义闪屏页

文章目录

大体介绍

文件介绍

其中:

  • pages为我们的页面内容(我个人理解功能性小于activity但是大于fragment)
  • route_map.json 为自定义的路由表(使用Navigation 而不是Router
  • SplashPage.ets 为我们自定义的闪屏文件
  • HomePage.ets 为我们测试的二级页面
  • route_map.json 需要配置在module.json5中

各部分代码

SplashPage.ets

bash 复制代码
import { router } from "@kit.ArkUI"

@Entry
@Component
struct SplashPage {
  build() {
    Column() {
      Image($r("app.media.huawei_logo"))
        .padding({
          left: "15%",
          right: "15%"
        }).width("100%")

      LoadingProgress()
        .width("20%")
        .height("20%")
        .color(Color.Red)
    }.height('100%')
    .width('100%')
    .alignItems(HorizontalAlign.Center)
    .justifyContent(FlexAlign.Center)
  }

  aboutToAppear(): void {
    setTimeout(() => {
      router.pushUrl(
        { url: "pages/Index" }
      ).then(()=>{
        router.clear()
      })
    }, 2000)
  }
}

Index.ets

bash 复制代码
@Entry
@Component
struct Index {
  @Provide('pageInfos') pageInfos: NavPathStack = new NavPathStack();



  build() {
    Navigation(this.pageInfos) {
        Column(){
          Button("TO HOME").onClick(()=>{
            this.pageInfos.pushPathByName("HomePage",null,true)
          })
        }
    }
    .height('100%')
    .width('100%')
    .mode(NavigationMode.Stack)
    .hideBackButton(true)
    .titleMode(NavigationTitleMode.Mini)
    .toolbarConfiguration([

    ])
  }
}

HomePage.ets

bash 复制代码
@Builder
export function PageOnBuilder(){
  HomePage()
}

@Component
struct HomePage {
  @State message: string = 'this is home';

  build() {
    NavDestination(){
      RelativeContainer() {
        Text(this.message)
          .id('HomePageHelloWorld')
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .alignRules({
            center: { anchor: '__container__', align: VerticalAlign.Center },
            middle: { anchor: '__container__', align: HorizontalAlign.Center }
          })
      }
      .height('100%')
      .width('100%')
    }
  }
}

route_map.json

bash 复制代码
{
  "routerMap": [
    {
      "name": "HomePage",
      "pageSourceFile": "src/main/ets/pages/HomePage.ets",
      "buildFunction": "PageOnBuilder"
    }
  ]
}

module.json5

bash 复制代码
 {
  "module": {
  	...
    "routerMap": "$profile:route_map",
    ...
    ]
  }
}

流程

  1. 修改启动页为自定义的Splash
  2. 自定义操作后跳到真正的Index页面
  3. Index页面用Navigation管理页面路由
相关推荐
SUPER52661 小时前
FastApi项目启动失败 got an unexpected keyword argument ‘loop_factory‘
java·服务器·前端
sanx181 小时前
专业电竞体育数据与系统解决方案
前端·数据库·apache·数据库开发·时序数据库
你的人类朋友3 小时前
【Node】认识一下Node.js 中的 VM 模块
前端·后端·node.js
Cosolar3 小时前
FunASR 前端语音识别代码解析
前端·面试·github
SmartBrain5 小时前
深入洞察:V模型架构实现业务到IT的服务化设计
华为·架构·创业创新
@大迁世界6 小时前
Vue 设计模式 实战指南
前端·javascript·vue.js·设计模式·ecmascript
芭拉拉小魔仙6 小时前
Vue项目中如何实现表格选中数据的 Excel 导出
前端·vue.js·excel
jump_jump6 小时前
妙用 localeCompare 获取汉字拼音首字母
前端·javascript·浏览器
U.2 SSD7 小时前
Echarts单轴坐标系散点图
前端·javascript·echarts
德育处主任Pro7 小时前
前端玩转大模型,DeepSeek-R1 蒸馏 Llama 模型的 Bedrock 部署
前端·llama