前言
在鸿蒙应用开发中,良好的窗口管理是提升用户体验的关键。本文将结合实战案例,详细介绍如何使用ArkTS实现:
- 沉浸式状态栏设置
- 系统栏颜色自定义
- 状态栏和导航栏高度获取
- 数据持久化存储
文章目录
一、核心API介绍
1.1 窗口管理API
鸿蒙系统提供了强大的窗口管理API,主要包括:
@ohos.window
:提供窗口管理相关功能setWindowLayoutFullScreen
:设置沉浸式状态getWindowAvoidArea
:获取系统栏区域setWindowSystemBarProperties
:设置系统栏属性
1.2 数据存储API
为了在应用的不同组件中都能方便地访问数据,我们使用:
- AppStorage:实现应用内数据共享
- PersistentStorage:实现数据持久化
二、WindowManager工具类实现
2.1 完整代码实现
import window from '@ohos.window';
export class WindowManager {
static fullScreen(windowStage: window.WindowStage) {
try {
const win = windowStage.getMainWindowSync();
// 开启沉浸式模式
win.setWindowLayoutFullScreen(true);
// 获取状态栏区域
let systemArea = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
// 获取顶部状态栏高度并转换为vp单位
let statusBarHeight = px2vp(systemArea.topRect.height);
// 获取导航条区域
let navigationArea = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR);
// 获取底部导航条高度并转换为vp单位
let navigationBarHeight = px2vp(navigationArea.bottomRect.height);
// 将高度值持久化存储
AppStorage.setOrCreate('statusBarHeight', statusBarHeight);
AppStorage.setOrCreate('navigationBarHeight', navigationBarHeight);
// 同时进行持久化存储
PersistentStorage.persistProp('statusBarHeight', statusBarHeight);
PersistentStorage.persistProp('navigationBarHeight', navigationBarHeight);
} catch (error) {
console.error('设置沉浸式状态失败:', error);
}
}
// 设置系统栏颜色
static setSystemBarColor(win: window.Window) {
try {
win.setWindowSystemBarProperties({
statusBarContentColor: '#23282C', // 状态栏文字颜色
navigationBarContentColor: '#23282C', // 导航栏文字颜色
statusBarColor: '#ff0000', // 状态栏背景色
navigationBarColor: '#ff0000', // 导航栏背景色
isStatusBarLightIcon: true, // 状态栏图标是否使用浅色
isNavigationBarLightIcon: true // 导航栏图标是否使用浅色
});
} catch (error) {
console.error('设置系统栏颜色失败:', error);
}
}
}
三、功能详解
3.1 沉浸式状态栏设置
fullScreen
方法实现了以下功能:
- 开启沉浸式模式
- 获取状态栏高度
- 获取导航栏高度
- 数据持久化存储
3.2 系统栏颜色设置
setSystemBarColor
方法支持自定义:
- 状态栏文字颜色
- 导航栏文字颜色
- 状态栏背景色
- 导航栏背景色
- 图标颜色模式
四、在EntryAbility中集成
import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { WindowManager } from '../utils/WindowManager';
export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage: window.WindowStage): void {
// 调用WindowManager的fullScreen方法设置沉浸式状态
WindowManager.fullScreen(windowStage);
// 获取主窗口并设置系统栏颜色
const win = windowStage.getMainWindowSync();
WindowManager.setSystemBarColor(win);
// 加载主页面
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
console.error('Failed to load the content. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in loading the content.');
});
}
}
4.1 默认状态展示
图1:展示了鸿蒙应用中默认的状态栏和导航栏布局,系统预留了顶部状态栏和底部导航栏的空间
五、使用示例
5.1 在页面中获取状态栏高度
@Entry
@Component
struct Index {
@StorageProp('statusBarHeight') statusBarHeight: number = 0;
@StorageProp('navigationBarHeight') navigationBarHeight: number = 0;
build() {
Column() {
// 顶部状态栏区域
Text('状态栏在这里')
.fontSize(20)
// 中间内容区域
Column() {
Text(`状态栏高度:${this.statusBarHeight}vp`)
.fontSize(20)
.margin(10)
Text(`导航栏高度:${this.navigationBarHeight}vp`)
.fontSize(20)
.margin(10)
}
.width('100%')
.layoutWeight(1)
.justifyContent(FlexAlign.Center)
// 底部导航栏区域
Text('导航栏在这里')
.fontSize(20)
}
.width('100%')
.height('100%')
.backgroundColor('#ccc')
}
}
5.2 沉浸式效果展示
图2:展示了沉浸式状态下的应用界面
手势导航模式下的导航栏实际占位但设置不生效,因此颜色设置无效
5.3 系统栏颜色设置效果
图3:去除颜色设置,启用padding间隔
注意:要启用系统栏颜色设置,需要:
- 在
EntryAbility
中取消注释系统栏颜色设置代码- 在
Index
组件中取消注释padding设置代码
六、最佳实践
6.1 窗口管理最佳实践
-
合理使用沉浸式状态栏
- 根据应用场景决定是否使用
- 注意内容不被状态栏遮挡
-
系统栏颜色设置
- 根据应用主题选择合适的颜色
- 确保文字和图标清晰可见
-
数据持久化
- 合理使用AppStorage和PersistentStorage
- 注意数据的更新时机
6.2 错误处理
- 使用try-catch捕获异常
- 提供合适的错误提示
- 设置默认值避免空值
七、常见问题
7.1 沉浸式状态栏问题
-
状态栏设置失败
- 检查权限设置
- 确认API调用时机
-
内容被状态栏遮挡
- 使用状态栏高度进行适配
- 合理设置内容边距
7.2 系统栏颜色问题
-
颜色设置不生效
- 检查颜色值格式
- 确认调用时机
-
图标显示异常
- 检查isStatusBarLightIcon设置
- 确保背景色和图标颜色对比度合适
八、总结
本文详细介绍了鸿蒙ArkTS中窗口管理的实现方法,包括:
- WindowManager工具类的实现
- 沉浸式状态栏的设置
- 系统栏颜色的自定义
- 状态栏和导航栏高度的获取
- 数据持久化的处理
通过本文的学习,相信读者能够更好地处理鸿蒙应用开发中的窗口管理问题。