HarmonyOS鸿蒙Next中setWindowSystemBarProperties修改状态栏颜色
HarmonyOS鸿蒙Next中setWindowSystemBarProperties修改状态栏颜色
调用系统api setWindowSystemBarProperties 修改状态栏颜色 , APP上滑动,状态栏还会是当前app设置的颜色,这种有修改状态栏颜色的需求,是调用系统api,还是自己写组件
3 回复
你的意思,APP上滑动后,应该把设置的状态栏颜色恢复默认,只有app打开的时候才显示设置的样式是吗?
可以设置下窗口为全屏布局,不去设置setWindowSystemBarProperties,
mainWindowClass.setWindowLayoutFullScreen(true, (err: BusinessError) => {
let errCode: number = err.code;
if (errCode) {
console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in setting the window layout to full-screen mode.');
});
更多关于HarmonyOS鸿蒙Next中setWindowSystemBarProperties修改状态栏颜色的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,setWindowSystemBarProperties方法用于设置窗口系统栏(包括状态栏和导航栏)的属性,包括颜色、可见性等。通过调用该方法,开发者可以自定义状态栏的背景颜色,以匹配应用的主题或设计需求。
具体使用方式如下:
- 获取窗口对象:首先需要通过
window模块获取当前窗口对象。 - 调用
setWindowSystemBarProperties方法:通过传递SystemBarProperties对象来设置状态栏的颜色。
示例代码:
import window from '@ohos.window';
let windowClass = null;
window.getTopWindow((err, data) => {
if (err) {
console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err));
return;
}
windowClass = data;
let sysBarProps = {
statusBarColor: '#FF0000', // 设置状态栏颜色为红色
navigationBarColor: '#00FF00' // 设置导航栏颜色为绿色
};
windowClass.setWindowSystemBarProperties(sysBarProps, (err) => {
if (err) {
console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in setting the system bar properties.');
});
});
在上述代码中,statusBarColor属性用于设置状态栏的背景颜色,navigationBarColor属性用于设置导航栏的背景颜色。颜色值可以是十六进制格式的颜色代码。
通过setWindowSystemBarProperties方法,开发者可以灵活地调整系统栏的外观,以增强应用的用户体验。


