鸿蒙使用沉浸式模式美化页面显示

鸿蒙使用沉浸式模式美化页面显示

01.页面沉浸式模式

未开启沉浸式
在这里插入图片描述
开启后
在这里插入图片描述

aboutToAppear(): void {
    //获取当前页面 并且设置为全屏显示(沉浸式显示模式)
    //getContext 获取上下文
    window.getLastWindow(getContext())
        .then(win=>win.setWindowLayoutFullScreen(true))
}

如果只设置给单一页面 会导致tabbar抖动(把tabbar往下挤开) 所以需要写在主页面让所有页面一开始都全部开启沉浸式模式 但是有些页面不需要开启 就需要获取安全区的高度 来用padding把内容挤下来

未开启沉浸式的tabbar
在这里插入图片描述
开启沉浸式后的tabbar
在这里插入图片描述

02.获取安全区高度+设置安全区文字颜色

01.安全区高度

aboutToAppear(): void {
    //获取当前页面 并且设置为全屏显示(沉浸式显示模式)
    //getContext 获取上下文
    window.getLastWindow(getContext())
        .then(win=>{
            win.setWindowLayoutFullScreen(true)
            //获取安全高度
            let area = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM)
            let hight = area.topRect.height
            //获取到的安全高度是px需要转为vp
            let vpHight = px2vp(hight)
            //存储vpHight 方便给其他组件使用
            AppStorage.setOrCreate('vpHight',vpHight)
        })
}
  @StorageLink('topHeight') topHeight: number = 0

02.设置安全区颜色

设置一个页面的安全区的颜色所有的页面的安全区颜色都会变所以需要在 aboutToDisappear () 中把颜色重新设置回去

aboutToAppear(): void {
    //设置安全区文字颜色
    window.getLastWindow(getContext())
        .then(win=>{
            win.setWindowSystemBarProperties({statusBarContentColor:'#FFFFFF'})
        })
}

03.封装沉浸式代码

import { window } from '@kit.ArkUI'

export class windowManager {
    //是否开启沉浸式模式
    static isFullScreen (isopen:boolean){
        //获取当前页面 并且设置为全屏显示(沉浸式显示模式)
        //getContext 获取上下文
        window.getLastWindow(getContext())
            .then(win=>{
            win.setWindowLayoutFullScreen(isopen)
            if(isopen){
                //获取安全高度
                let area = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM)
                let hight = area.topRect.height
                //获取到的安全高度是px需要转为vp
                let vpHight = px2vp(hight)
                //存储vpHight 方便给其他组件使用
                AppStorage.setOrCreate('vpHight',vpHight)
            }else {
                AppStorage.setOrCreate('vpHight',0)
            }
        })
    }
    //改变安全区文字颜色
    static changeColor (color:string){
        window.getLastWindow(getContext())
            .then(win=>{
            win.setWindowSystemBarProperties({statusBarContentColor:color})
        })
    }
}
  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值