文档管理中心

如何实现沉浸式页面(包括沉浸式状态栏、沉浸式导航条)

解决措施

沉浸式页面开发分为以下两步:

  1. 实现沉浸式效果。
    • 方案一:使用setWindowLayoutFullScreen()方法设置窗口为全屏模式。
      收起
      自动换行
      深色代码主题
      复制
      1. import { UIAbility } from '@kit.AbilityKit';
      2. import { window } from '@kit.ArkUI';
      3. import { BusinessError } from '@kit.BasicServicesKit';
      4. export default class EntryAbility extends UIAbility {
      5. onWindowStageCreate(windowStage: window.WindowStage) {
      6. // 1.Get the main window of the application.
      7. let windowClass: window.Window | undefined = undefined;
      8. windowStage.getMainWindow().then(windowClass => {
      9. console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(windowClass));
      10. // 2.Set the window to full screen to achieve immersive effects.
      11. windowClass.setWindowLayoutFullScreen(true).then(() => {
      12. console.info('Succeeded in setting the window layout to full-screen mode.');
      13. }).catch((e: BusinessError) => {
      14. console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(e));
      15. })
      16. }).catch((err: BusinessError) => {
      17. console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err));
      18. })
      19. // 3.Load the corresponding target page for the immersive window.
      20. windowStage.loadContent("pages/Index", (err) => {
      21. if (err.code) {
      22. console.error('Failed to load the content. Cause:' + JSON.stringify(err));
      23. return;
      24. }
      25. console.info('Succeeded in loading the content.');
      26. });
      27. }
      28. };
    • 方案二:设置组件的expandSafeArea属性,扩展组件的安全区域到状态栏和导航栏,从而实现沉浸式。
      收起
      自动换行
      深色代码主题
      复制
      1. @Entry
      2. @Component
      3. struct Example {
      4. build() {
      5. Column() {
      6. Row() {
      7. Text('Top Row')
      8. .fontSize(40)
      9. .textAlign(TextAlign.Center)
      10. .width('100%')
      11. }
      12. .backgroundColor('#F08080')
      13. // Set the top drawing to extend to the status bar
      14. .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP])
      15. Row() {
      16. Text('ROW2')
      17. .fontSize(40)
      18. }
      19. .backgroundColor(Color.Orange)
      20. .padding(20)
      21. Row() {
      22. Text('ROW3')
      23. .fontSize(40)
      24. }
      25. .backgroundColor(Color.Orange)
      26. .padding(20)
      27. Row() {
      28. Text('ROW4')
      29. .fontSize(40)
      30. }
      31. .backgroundColor(Color.Orange)
      32. .padding(20)
      33. Row() {
      34. Text('ROW5')
      35. .fontSize(40)
      36. }
      37. .backgroundColor(Color.Orange)
      38. .padding(20)
      39. Row() {
      40. Text('Bottom Row')
      41. .fontSize(40)
      42. .textAlign(TextAlign.Center)
      43. .width('100%')
      44. }
      45. .backgroundColor(Color.Orange)
      46. // Set the bottom drawing to extend to the navigation bar
      47. .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.BOTTOM])
      48. }
      49. .width('100%')
      50. .height('100%')
      51. .alignItems(HorizontalAlign.Center)
      52. .backgroundColor('#008000')
      53. .justifyContent(FlexAlign.SpaceBetween)
      54. }
      55. }
  2. 处理避让区域与页面内容的适配问题。

    避让区包含状态栏中的电量、时间等系统信息,或手势交互如导航栏点击或上滑。实现应用页面沉浸式效果后,可能会与避让区产生UI元素遮挡、视觉违和或交互冲突等问题。开发者可针对不同场景选择以下方式对避让区和应用页面进行适配。

参考链接

窗口沉浸式