OpenHarmony 5.0设置应用设置手势导航开关打开后重新关闭导航栏和设置界面重合

目录

1.背景

2.解决方案


1.背景

在OpenHarmony 5.0中从设置界面打开手势导航开关然后重新关闭,此时设置界面导航栏和设置列表主界面重合,导致设置界面无法点击最下面的关于设备

2.解决方案

首先参考之前的如何设置导航栏文档,我们可以自己再重新设置下导航栏样式及是否显示,然后在设置是否开始手势导航有一个

复制代码
settings.display.navigationbar_status

属性,我们根据这个属性强制显示和隐藏导航栏

最终的patch如下:

复制代码
@@ -22,8 +22,14 @@ import AbilityConstant from '@ohos.app.ability.AbilityConstant';
 import window from '@ohos.window';
 import router from '@ohos.router';
 import { UIContext } from '@ohos.arkui.UIContext';
+import { BusinessError } from '@ohos.base';
+import settings from '@ohos.settings';
+
 export default class MainAbility extends Ability {
   private funcAbilityWant;
+  private windowStage:window.WindowStage;
+  private isBackground = false
+
   onCreate(want, launchParam) {
     AppStorage.SetOrCreate('newWant', want);
 
@@ -39,10 +45,61 @@ export default class MainAbility extends Ability {
   }
   onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam) {
     AppStorage.SetOrCreate('newWant', want);
-
+    this.setNavigationBarStatus();
+    setTimeout(()=>{
+      this.setNavigationBarStatus();
+    },500);
     this.toDetailPageFormUrl(want)
   }
 
+  private setNavigationBarStatus() {
+    if (this.isBackground) {
+      return
+    }
+    const initValue = settings.getValueSync(this.context, 'settings.display.navigationbar_status', '1');
+    const navigationBarStatusValue = initValue === '0' ? false : true;
+    LogUtil.error("navigationBarStatusValue"+navigationBarStatusValue);
+    if (this.windowStage) {
+      let windowClass: window.Window | undefined = undefined;
+      this.windowStage.getMainWindow((err: BusinessError, data) => {
+        const errCode: number = err.code;
+        if (errCode) {
+          console.error(`Failed to obtain the main window. Cause code: ${err.code}, message: ${err.message}`);
+          return;
+        }
+
+        // 2.实现沉浸式效果:设置导航栏、状态栏显示。
+        // let names = ['status', 'navigation'];
+        // 2.实现沉浸式效果:设置导航栏、状态栏不显示。
+        let names: Array<'status' | 'navigation'>  = ['status'];
+        if (navigationBarStatusValue) {
+          names = ['status', 'navigation'];
+        }
+
+        windowClass = data;
+        windowClass.setWindowSystemBarEnable(names);
+        let SystemBarProperties: window.SystemBarProperties = {
+          //状态栏和导航栏背景颜色
+          statusBarColor: '#ffffff',
+          navigationBarColor: '#ffffff',
+          //以下两个属性从API Version8开始支持,状态栏和导航栏内容颜色
+          statusBarContentColor: '#000000',
+          navigationBarContentColor: '#000000'
+        };
+        try {
+          let promise = windowClass.setWindowSystemBarProperties(SystemBarProperties);
+          promise.then(() => {
+            console.info('Succeeded in setting the system bar properties.');
+          }).catch((err: BusinessError) => {
+            console.error(`Failed to set the system bar properties. Cause code: ${err.code}, message: ${err.message}`);
+          });
+        } catch (exception) {
+          console.error(`Failed to set the system bar properties. Cause code: ${exception.code}, message: ${exception.message}`);
+        }
+      });
+    }
+  }
+
   toDetailPageFormUrl(want) {
     window.getLastWindow(this.context).then((window: window.Window) => {
       let UIContext: UIContext = window.getUIContext();
@@ -54,6 +111,7 @@ export default class MainAbility extends Ability {
       }, router.RouterMode.Single)
     })
   }
+
   onWindowStageCreate(windowStage) {
     // Main window is created, set main page for this ability
     LogUtil.log("[Main] MainAbility onWindowStageCreate")
@@ -76,6 +134,8 @@ export default class MainAbility extends Ability {
     GlobalContext.getContext().setObject(GlobalContext.globalKeySettingsAbilityContext, this.context);
     let want = GlobalContext.getContext().getObject(GlobalContext.globalKeyAbilityWant) as Want
     this.toDetailPageFormUrl(want)
+    this.windowStage = windowStage
+
   }
 
   onWindowStageDestroy() {
@@ -86,10 +146,12 @@ export default class MainAbility extends Ability {
   onForeground() {
     // Ability has brought to foreground
     LogUtil.log("[Main] MainAbility onForeground")
+    this.isBackground = false
   }
 
   onBackground() {
     // Ability has back to background
     LogUtil.log("[Main] MainAbility onBackground")
+    this.isBackground = true
   }
 };
相关推荐
不爱吃糖的程序媛28 分钟前
适配鸿蒙PC sha_ohos.patch 补丁文件详解
华为·harmonyos
AI_零食32 分钟前
声音分贝模拟与波动动画展示:鸿蒙Flutter框架 实现的声音可视化应用
学习·flutter·华为·开源·harmonyos
独特的螺狮粉2 小时前
Flutter 框架跨平台鸿蒙开发 - 关系测试应用
flutter·华为·架构·开源·鸿蒙
SoraLuna2 小时前
「鸿蒙智能体实战记录 10」节气知识展示卡片开发与工作流绑定实现
华为·harmonyos
autumn20053 小时前
Flutter 框架跨平台鸿蒙开发 - 颜色情绪
flutter·华为·harmonyos
Utopia^3 小时前
Flutter 框架跨平台鸿蒙开发 - 记忆热力图
flutter·华为·harmonyos
2301_822703203 小时前
Flutter 框架跨平台鸿蒙开发 - 创意声音合成器应用
算法·flutter·华为·harmonyos·鸿蒙
李李李勃谦4 小时前
Flutter 框架跨平台鸿蒙开发 - 创意灵感收集
android·flutter·harmonyos
2301_822703205 小时前
Flutter 框架跨平台鸿蒙开发 - 气味记忆唤醒应用
flutter·华为·开源·harmonyos·鸿蒙
AI_零食6 小时前
开源鸿蒙跨平台Flutter开发:脑筋急转弯应用开发文档
flutter·华为·开源·harmonyos·鸿蒙