HarmonyOS 鸿蒙Next如何设置沉浸式窗口
HarmonyOS 鸿蒙Next如何设置沉浸式窗口
问题现象
如何设置沉浸式窗口。
解决措施
在EntryAbility的onWindowStageCreate方法中通过windowStage获取window,然后分别调用setWindowLayoutFullScreen和setWindowSystemBarEnable方法。
参考代码
import { UIAbility } from ‘@kit.AbilityKit’;
import { window } from ‘@kit.ArkUI’;
import { BusinessError } from ‘@kit.BasicServicesKit’;
export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage: window.WindowStage) {
// 1.获取应用主窗口。
let windowClass: window.Window | null = null;
windowStage.getMainWindow((err: BusinessError, data) => {
let errCode: number = err.code;
if (errCode) {
console.error('Failed to obtain the main window. Cause: ’ + JSON.stringify(err));
return;
}
windowClass = data;
console.info('Succeeded in obtaining the main window. Data: ’ + JSON.stringify(data));
<span class="hljs-comment"><span class="hljs-comment">// 2.实现沉浸式效果。方式一:设置导航栏、状态栏不显示。 </span></span>
<span class="hljs-keyword"><span class="hljs-keyword">let</span></span> names: <span class="hljs-built_in"><span class="hljs-built_in">Array</span></span><<span class="hljs-string"><span class="hljs-string">'status'</span></span> | <span class="hljs-string"><span class="hljs-string">'navigation'</span></span>> = [];
windowClass.setWindowSystemBarEnable(names, (err: BusinessError) => {
<span class="hljs-keyword"><span class="hljs-keyword">let</span></span> errCode: number = err.code;
<span class="hljs-keyword"><span class="hljs-keyword">if</span></span> (errCode) {
console.error(<span class="hljs-string"><span class="hljs-string">'Failed to set the system bar to be visible. Cause:'</span></span> + <span class="hljs-built_in"><span class="hljs-built_in">JSON</span></span>.stringify(err));
<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>;
}
console.info(<span class="hljs-string"><span class="hljs-string">'Succeeded in setting the system bar to be visible.'</span></span>);
});
<span class="hljs-comment"><span class="hljs-comment">// 2.实现沉浸式效果。方式二:设置窗口为全屏布局,配合设置导航栏、状态栏的透明度、背景/文字颜色及高亮图标等属性,与主窗口显示保持协调一致。 </span></span>
<span class="hljs-keyword"><span class="hljs-keyword">let</span></span> isLayoutFullScreen = <span class="hljs-literal"><span class="hljs-literal">true</span></span>;
windowClass.setWindowLayoutFullScreen(isLayoutFullScreen, (err: BusinessError) => {
<span class="hljs-keyword"><span class="hljs-keyword">let</span></span> errCode: number = err.code;
<span class="hljs-keyword"><span class="hljs-keyword">if</span></span> (errCode) {
console.error(<span class="hljs-string"><span class="hljs-string">'Failed to set the window layout to full-screen mode. Cause:'</span></span> + <span class="hljs-built_in"><span class="hljs-built_in">JSON</span></span>.stringify(err));
<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>;
}
console.info(<span class="hljs-string"><span class="hljs-string">'Succeeded in setting the window layout to full-screen mode.'</span></span>);
});
<span class="hljs-keyword"><span class="hljs-keyword">let</span></span> sysBarProps: window.SystemBarProperties = {
statusBarColor: <span class="hljs-string"><span class="hljs-string">'#ff00ff'</span></span>,
navigationBarColor: <span class="hljs-string"><span class="hljs-string">'#00ff00'</span></span>,
statusBarContentColor: <span class="hljs-string"><span class="hljs-string">'#ffffff'</span></span>,
navigationBarContentColor: <span class="hljs-string"><span class="hljs-string">'#ffffff'</span></span>
};
windowClass.setWindowSystemBarProperties(sysBarProps, (err: BusinessError) => {
<span class="hljs-keyword"><span class="hljs-keyword">let</span></span> errCode: number = err.code;
<span class="hljs-keyword"><span class="hljs-keyword">if</span></span> (errCode) {
console.error(<span class="hljs-string"><span class="hljs-string">'Failed to set the system bar properties. Cause: '</span></span> + <span class="hljs-built_in"><span class="hljs-built_in">JSON</span></span>.stringify(err));
<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>;
}
console.info(<span class="hljs-string"><span class="hljs-string">'Succeeded in setting the system bar properties.'</span></span>);
});
})
<span class="hljs-comment"><span class="hljs-comment">// 3.为沉浸式窗口加载对应的目标页面。 </span></span>
windowStage.loadContent(<span class="hljs-string"><span class="hljs-string">"pages/page2"</span></span>, (err: BusinessError) => {
<span class="hljs-keyword"><span class="hljs-keyword">let</span></span> errCode: number = err.code;
<span class="hljs-keyword"><span class="hljs-keyword">if</span></span> (errCode) {
console.error(<span class="hljs-string"><span class="hljs-string">'Failed to load the content. Cause:'</span></span> + <span class="hljs-built_in"><span class="hljs-built_in">JSON</span></span>.stringify(err));
<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>;
}
console.info(<span class="hljs-string"><span class="hljs-string">'Succeeded in loading the content.'</span></span>);
});
}
};
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
参考链接
HarmonyOS 鸿蒙Next设置沉浸式窗口,可以通过以下步骤实现:
-
调用主窗口接口:
- 在
onWindowStageCreate
方法中,获取应用启动时的初始布局避让区域,并调用windowStage.getMainWindowSync()
获取主窗口对象。 - 使用
setWindowLayoutFullScreen(true)
方法将窗口设置为沉浸式布局。
- 在
-
设置窗口背景色:
- 通过
setWindowBackgroundColor(color)
方法设置窗口的背景颜色,颜色值采用十六进制表示。
- 通过
-
自定义状态栏和导航栏(可选):
- 使用
setWindowSystemBarProperties(systemBarProperties)
方法自定义状态栏和导航栏的属性,包括背景颜色、文字颜色以及图标的高亮状态。
- 使用
-
处理安全区域:
- 使用
getWindowAvoidArea
方法获取状态栏和导航栏等需要避让的区域,并在布局中做相应处理,以确保内容不会与这些区域重叠。
- 使用
鸿蒙Next教程已发布,可以先学学:https://www.itying.com/category-93-b0.html