import { window } from '@kit.ArkUI'
class FullScreen{
//开启全屏
async enable(){
const ctx=AppStorage.get<Context>('context')!
const win=await window.getLastWindow(ctx)
win.setWindowLayoutFullScreen(true)
//顶部安全区高度(状态栏)
const topRect=win.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM).topRect
AppStorage.setOrCreate<number>('topHeight',px2vp(topRect.height))
//底部导航的高度(导航条)
const bottomRect=win.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR).bottomRect
AppStorage.setOrCreate<number>('bottomHeight',px2vp(bottomRect.height))
}
//关闭全屏
async disable(){
const ctx=AppStorage.get<Context>('context')!
const win=await window.getLastWindow(ctx)
win.setWindowLayoutFullScreen(false)
//false关闭沉浸式
//顶部安全区高度(状态栏)归零
AppStorage.setOrCreate<number>('topHeight',0)
//底部导航的高度归零
AppStorage.setOrCreate<number>('bottomHeight',0)
}
}
export const fullScreen=new FullScreen()
封装一个FullScreen组件,在EntryAbility中找到onWindowStageCreate,若想设置全屏则在onWindowStageCreate中写fullScreen.enable(),若想关闭全屏则写fullScreen.disable()。
标签:FullScreen,AppStorage,const,沉浸,win,setOrCreate,HarmonyOS,window From: https://blog.csdn.net/LexinZong/article/details/141866503