未验证 提交 84815acf 编写于 作者: O openharmony_ci 提交者: Gitee

!12317 master:windowmanager使用最新接口:需同步翻译

Merge pull request !12317 from 葛亚芳/master
......@@ -46,17 +46,15 @@ import window from '@ohos.window';
## Configuration<sup>9+</sup>
创建子窗口时的参数。
当Context为[ServiceExtensionContext](js-apis-inner-application-serviceExtensionContext.md)时,创建系统窗口时,使用callback异步回调。
创建子窗口或系统窗口时的参数。
**系统能力:** SystemCapability.WindowManager.WindowManager.Core
| 名称 | 类型 | 必填 | 说明 |
| ---------- | -------------------------- | -- | ----------------------------------- |
| name | string | 是 | 窗口名字。 |
| windowType | [WindowType](#windowtype7) | 是 | 窗口类型。 |
| ctx | BaseContext | 否 | 当前应用上下文信息。<br>FA模型的Context定义见[Context](js-apis-inner-app-context.md)<br>Stage模型的Context定义见[Context](js-apis-inner-application-serviceExtensionContext.md)。不设置,则默认为空。 |
| name | string | 是 | 窗口名字。 |
| windowType | [WindowType](#windowtype7) | 是 | 窗口类型。 |
| ctx | BaseContext | 否 | 当前应用上下文信息。<br>FA模型的Context定义见[Context](js-apis-inner-app-context.md)<br>Stage模型的Context定义见[ServiceExtensionContext](js-apis-inner-application-serviceExtensionContext.md)。不设置,则默认为空。<br>当Context为[ServiceExtensionContext](js-apis-inner-application-serviceExtensionContext.md)时,创建系统窗口。 |
| displayId | number | 否 | 当前物理屏幕id。不设置,则默认为-1。 |
| parentId | number | 否 | 父窗口id。不设置,则默认为-1。 |
......@@ -302,7 +300,7 @@ import window from '@ohos.window';
createWindow(config: Configuration, callback: AsyncCallback&lt;Window&gt;): void
创建子窗口,使用callback异步回调。
创建子窗口或者系统窗口,使用callback异步回调。
**系统能力:** SystemCapability.WindowManager.WindowManager.Core
......@@ -310,8 +308,8 @@ createWindow(config: Configuration, callback: AsyncCallback&lt;Window&gt;): void
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------------------------- | -- | --------------------------------- |
| config | [Configuration](#configuration9) | 是 | 当前应用上下文信息。 |
| callback | AsyncCallback&lt;[Window](#window)&gt; | 是 | 回调函数。返回当前创建的窗口对象。 |
| config | [Configuration](#configuration9) | 是 | 创建窗口时的参数。 |
| callback | AsyncCallback&lt;[Window](#window)&gt; | 是 | 回调函数。返回当前创建的窗口对象。 |
**错误码:**
......@@ -346,7 +344,7 @@ try {
createWindow(config: Configuration): Promise&lt;Window&gt;
创建子窗口,使用Promise异步回调。
创建子窗口或者系统窗口,使用Promise异步回调。
**系统能力:** SystemCapability.WindowManager.WindowManager.Core
......@@ -354,13 +352,13 @@ createWindow(config: Configuration): Promise&lt;Window&gt;
| 参数名 | 类型 | 必填 | 说明 |
| ------ | -------------------------------- | -- | ------------------ |
| config | [Configuration](#configuration9) | 是 | 当前应用上下文信息。 |
| config | [Configuration](#configuration9) | 是 | 创建窗口时的参数。 |
**返回值:**
| 类型 | 说明 |
| -------------------------------- | ------------------------------------ |
| Promise&lt;[Window](#window)&gt; | Promise对象。返回当前创建的窗口对象。 |
| Promise&lt;[Window](#window)&gt; | Promise对象。返回当前创建的窗口对象。 |
**错误码:**
......@@ -393,7 +391,7 @@ try {
findWindow(name: string): Window
查找id所对应的窗口。
查找name所对应的窗口。
**系统能力:** SystemCapability.WindowManager.WindowManager.Core
......@@ -913,7 +911,7 @@ create(ctx: BaseContext, id: string, type: WindowType, callback: AsyncCallback&l
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ |
| ctx | BaseContext | 是 | 当前应用上下文信息。<br>FA模型的Context定义见[Context](js-apis-inner-app-context.md)<br>Stage模型的Context定义见[Context](js-apis-inner-application-serviceExtensionContext.md)。 |
| ctx | BaseContext | 是 | 当前应用上下文信息。<br>FA模型的Context定义见[Context](js-apis-inner-app-context.md)<br>Stage模型的Context定义见[ServiceExtensionContext](js-apis-inner-application-serviceExtensionContext.md)。 |
| id | string | 是 | 窗口id。 |
| type | [WindowType](#windowtype7) | 是 | 窗口类型。 |
| callback | AsyncCallback&lt;[Window](#window)&gt; | 是 | 回调函数。返回当前创建的子窗口对象。 |
......@@ -951,7 +949,7 @@ create(ctx: BaseContext, id: string, type: WindowType): Promise&lt;Window&gt;
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------------------------- | ---- | ------------------------------------------------------------ |
| ctx | BaseContext | 是 | 当前应用上下文信息。<br/>FA模型的Context定义见[Context](js-apis-inner-app-context.md)<br/>Stage模型的Context定义见[Context](js-apis-inner-application-serviceExtensionContext.md)。 |
| ctx | BaseContext | 是 | 当前应用上下文信息。<br/>FA模型的Context定义见[Context](js-apis-inner-app-context.md)<br/>Stage模型的Context定义见[ServiceExtensionContext](js-apis-inner-application-serviceExtensionContext.md)。 |
| id | string | 是 | 窗口id。 |
| type | [WindowType](#windowtype7) | 是 | 窗口类型。 |
......
......@@ -22,21 +22,19 @@
| 实例名 | 接口名 | 描述 |
| -------- | -------- | -------- |
| window静态方法 | create(id: string, type: WindowType, callback: AsyncCallback&lt;Window&gt;): void | 创建子窗口。<br>此接口仅可在`FA`模型下使用。 |
| window静态方法 | getTopWindow(callback: AsyncCallback&lt;Window&gt;): void | 获取当前应用内最后显示的窗口。<br/>此接口仅可在`FA`模型下使用。 |
| window静态方法 | find(id: string, callback: AsyncCallback&lt;Window&gt;): void | 查找`id`所对应的窗口。 |
| Window | loadContent(path: string, callback: AsyncCallback&lt;void&gt;): void | 为当前窗口加载具体页面内容。 |
| Window | moveTo(x: number, y: number, callback: AsyncCallback&lt;void&gt;): void | 移动当前窗口。 |
| Window | setBackgroundColor(color: string, callback: AsyncCallback&lt;void&gt;): void | 设置窗口的背景色。 |
| Window | setBrightness(brightness: number, callback: AsyncCallback&lt;void&gt;): void | 设置屏幕亮度值。 |
| Window | resetSize(width: number, height: number, callback: AsyncCallback&lt;void&gt;): void | 改变当前窗口大小。 |
| Window | setFullScreen(isFullScreen: boolean, callback: AsyncCallback&lt;void&gt;): void | 设置窗口是否全屏显示。 |
| Window | setLayoutFullScreen(isLayoutFullScreen: boolean, callback: AsyncCallback&lt;void&gt;): void | 设置窗口布局是否为全屏布局。 |
| Window | setSystemBarEnable(names: Array&lt;'status'\|'navigation'&gt;): Promise&lt;void&gt; | 设置导航栏、状态栏是否显示。 |
| Window | setSystemBarProperties(systemBarProperties: SystemBarProperties, callback: AsyncCallback&lt;void&gt;): void | 设置窗口内导航栏、状态栏属性。<br/>`systemBarProperties`:导航栏、状态栏的属性集合。 |
| Window | show(callback: AsyncCallback\<void>): void | 显示当前窗口。 |
| window静态方法 | createWindow(config: Configuration, callback: AsyncCallback<Window>): void | 创建子窗口。<br/>-`config`:创建窗口时的参数。 |
| window静态方法 | findWindow(id: string, callback: AsyncCallback&lt;Window&gt;): void | 查找`id`所对应的窗口。 |
| Window | SetUIContent(path: string, callback: AsyncCallback&lt;void&gt;): void | 为当前窗口加载具体页面内容。 |
| Window | moveWindowTo(x: number, y: number, callback: AsyncCallback&lt;void&gt;): void | 移动当前窗口。 |
| Window | setWindowBackgroundColor(color: string, callback: AsyncCallback&lt;void&gt;): void | 设置窗口的背景色。 |
| Window | setWindowBrightness(brightness: number, callback: AsyncCallback&lt;void&gt;): void | 设置屏幕亮度值。 |
| Window | resize(width: number, height: number, callback: AsyncCallback&lt;void&gt;): void | 改变当前窗口大小。 |
| Window | setWindowLayoutFullScreen(isLayoutFullScreen: boolean, callback: AsyncCallback&lt;void&gt;): void | 设置窗口布局是否为全屏布局。 |
| Window | setWindowSystemBarEnable(names: Array&lt;'status'\|'navigation'&gt;): Promise&lt;void&gt; | 设置导航栏、状态栏是否显示。 |
| Window | setWindowSystemBarProperties(systemBarProperties: SystemBarProperties, callback: AsyncCallback&lt;void&gt;): void | 设置窗口内导航栏、状态栏属性。<br/>`systemBarProperties`:导航栏、状态栏的属性集合。 |
| Window | showWindow(callback: AsyncCallback\<void>): void | 显示当前窗口。 |
| Window | on(type: 'touchOutside', callback: Callback&lt;void&gt;): void | 开启本窗口区域外的点击事件的监听。 |
| Window | destroy(callback: AsyncCallback&lt;void&gt;): void | 销毁当前窗口。 |
| Window | destroyWindow(callback: AsyncCallback&lt;void&gt;): void | 销毁当前窗口。 |
## 设置应用子窗口
......@@ -48,16 +46,16 @@
1. 创建/获取子窗口对象。
- 可以通过`window.create`接口创建子窗口。
- 可以通过`window.getTopWindow`来获取最后显示的窗口得到子窗口。
- 也可以通过`window.find`接口来查找已经创建的窗口从而得到子窗口。
```js
- 可以通过`window.createWindow`接口创建子窗口。
- 也可以通过`window.findWindow`接口来查找已经创建的窗口从而得到子窗口。
```js
import window from '@ohos.window';
let windowClass = null;
// 方式一:创建子窗口。
window.create("subWindow", window.WindowType.TYPE_APP, (err, data) => {
let config = {name: "subWindow", windowType: window.WindowType.TYPE_APP, ctx: this.context};
window.createWindow(config, (err, data) => {
if (err.code) {
console.error('Failed to create the subWindow. Cause: ' + JSON.stringify(err));
return;
......@@ -65,17 +63,8 @@
console.info('Succeeded in creating subWindow. Data: ' + JSON.stringify(data));
windowClass = data;
});
// 方式二:获取子窗口。
window.getTopWindow((err, data) => {
if (err.code) {
console.error('Failed to get the subWindow. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in getting subWindow. Data: ' + JSON.stringify(data));
windowClass = data;
});
// 方式三:查找得到子窗口。
window.find("subWindow", (err, data) => {
// 方式二:查找得到子窗口。
window.findWindow("subWindow", (err, data) => {
if (err.code) {
console.error('Failed to find the subWindow. Cause: ' + JSON.stringify(err));
return;
......@@ -91,7 +80,7 @@
```js
// 移动子窗口位置。
windowClass.moveTo(300, 300, (err) => {
windowClass.moveWindowTo(300, 300, (err) => {
if (err.code) {
console.error('Failed to move the window. Cause:' + JSON.stringify(err));
return;
......@@ -99,7 +88,7 @@
console.info('Succeeded in moving the window.');
});
// 改变子窗口大小。
windowClass.resetSize(500, 1000, (err) => {
windowClass.resize(500, 500, (err) => {
if (err.code) {
console.error('Failed to change the window size. Cause:' + JSON.stringify(err));
return;
......@@ -110,18 +99,18 @@
3. 加载显示子窗口的具体内容。
使用`loadContent``show`接口加载显示子窗口的具体内容。
使用`SetUIContent``showWindow`接口加载显示子窗口的具体内容。
```js
// 为子窗口加载对应的目标页面。
windowClass.loadContent("pages/page2", (err) => {
windowClass.SetUIContent("pages/page2", (err) => {
if (err.code) {
console.error('Failed to load the content. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in loading the content.');
// 显示子窗口。
windowClass.show((err) => {
windowClass.showWindow((err) => {
if (err.code) {
console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
return;
......@@ -133,11 +122,11 @@
4. 销毁子窗口。
当不再需要某些子窗口时,可根据场景的具体实现逻辑,使用`destroy`接口销毁子窗口。
当不再需要某些子窗口时,可根据场景的具体实现逻辑,使用`destroyWindow`接口销毁子窗口。
```js
// 销毁子窗口。当不再需要某些子窗口时,可根据场景的具体实现逻辑,使用destroy接口销毁子窗口。
windowClass.destroy((err) => {
windowClass.destroyWindow((err) => {
if (err.code) {
console.error('Failed to destroy the subwindow. Cause:' + JSON.stringify(err));
return;
......@@ -160,14 +149,14 @@
>
> 沉浸式能力需要在成功获取应用主窗口对象的前提下进行。
>
> 确保应用内最后显示的窗口为主窗口,然后再使用`window.getTopWindow`接口来获取得到主窗口。
> 确保应用内最后显示的窗口为主窗口,然后再使用`window.getLastWindow`接口来获取得到主窗口。
```js
import window from '@ohos.window';
let mainWindowClass = null;
// 获取主窗口。
window.getTopWindow((err, data) => {
window.getLastWindow((err, data) => {
if (err.code) {
console.error('Failed to get the subWindow. Cause: ' + JSON.stringify(err));
return;
......@@ -179,23 +168,14 @@
2. 实现沉浸式效果。有以下三种方式:
- 方式一:调用`setFullScreen`接口,设置应用主窗口为全屏显示,此时导航栏、状态栏将隐藏,从而达到沉浸式效果。
- 方式二:调用`setSystemBarEnable`接口,设置导航栏、状态栏不显示,从而达到沉浸式效果。
- 方式三:调用`setLayoutFullScreen`接口,设置应用主窗口为全屏布局;然后调用`setSystemProperties`接口,设置导航栏、状态栏的透明度、背景/文字颜色以及高亮图标等属性,使之保持与主窗口显示协调一致,从而达到沉浸式效果。
- 方式一:调用`setWindowSystemBarEnable`接口,设置导航栏、状态栏不显示,从而达到沉浸式效果。
- 方式二:调用`setWindowLayoutFullScreen`接口,设置应用主窗口为全屏布局;然后调用`setSystemProperties`接口,设置导航栏、状态栏的透明度、背景/文字颜色以及高亮图标等属性,使之保持与主窗口显示协调一致,从而达到沉浸式效果。
```js
// 实现沉浸式效果。方式一:设置窗口全屏显示。
let isFullScreen = true;
mainWindowClass.setFullScreen(isFullScreen, (err) => {
if (err.code) {
console.error('Failed to enable the full-screen mode. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in enabling the full-screen mode.');
});
// 实现沉浸式效果。方式二:设置导航栏、状态栏不显示。
// 实现沉浸式效果。方式一:设置导航栏、状态栏不显示。
let names = [];
mainWindowClass.setSystemBarEnable(names, (err) => {
mainWindowClass.setWindowSystemBarEnable(names, (err) => {
if (err.code) {
console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err));
return;
......@@ -203,9 +183,9 @@
console.info('Succeeded in setting the system bar to be visible.');
});
// 实现沉浸式效果。
// 方式:设置窗口为全屏布局,配合设置状态栏、导航栏的透明度、背景/文字颜色及高亮图标等属性,与主窗口显示保持协调一致。
// 方式:设置窗口为全屏布局,配合设置状态栏、导航栏的透明度、背景/文字颜色及高亮图标等属性,与主窗口显示保持协调一致。
let isLayoutFullScreen = true;
mainWindowClass.setLayoutFullScreen(isLayoutFullScreen, (err) => {
mainWindowClass.setWindowLayoutFullScreen(isLayoutFullScreen, (err) => {
if (err.code) {
console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err));
return;
......@@ -219,7 +199,7 @@
statusBarContentColor: '#ffffff',
navigationBarContentColor: '#ffffff'
};
mainWindowClass.setSystemBarProperties(sysBarProps, (err) => {
mainWindowClass.setWindowSystemBarProperties(sysBarProps, (err) => {
if (err.code) {
console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err));
return;
......@@ -227,21 +207,21 @@
console.info('Succeeded in setting the system bar properties.');
});
```
3. 加载显示沉浸式窗口的具体内容。
使用`loadContent``show`接口加载显示沉浸式窗口的具体内容。
使用`SetUIContent``showWindow`接口加载显示沉浸式窗口的具体内容。
```js
// 为沉浸式窗口加载对应的目标页面。
mainWindowClass.loadContent("pages/page3", (err) => {
mainWindowClass.SetUIContent("pages/page3", (err) => {
if (err.code) {
console.error('Failed to load the content. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in loading the content.');
// 显示沉浸式窗口。
mainWindowClass.show((err) => {
mainWindowClass.showWindow((err) => {
if (err.code) {
console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
return;
......
......@@ -34,25 +34,24 @@
| WindowStage | getMainWindow(callback: AsyncCallback&lt;Window&gt;): void | 获取`WindowStage`实例下的主窗口。<br/>此接口仅可在`Stage`模型下使用。 |
| WindowStage | loadContent(path: string, callback: AsyncCallback&lt;void&gt;): void | 为当前`WindowStage`的主窗口加载具体页面。<br/>此接口仅可在`Stage`模型下使用。 |
| WindowStage | createSubWindow(name: string, callback: AsyncCallback&lt;Window&gt;): void | 创建子窗口。<br/>此接口仅可在`Stage`模型下使用。 |
| window静态方法 | create(ctx: Context, id: string, type: WindowType, callback: AsyncCallback&lt;Window&gt;): void | 创建子窗口。<br/>-`ctx`:为应用上下文信息。<br/>-`type`:为创建的窗口类型。 |
| Window | loadContent(path: string, callback: AsyncCallback&lt;void&gt;): void | 为当前窗口加载具体页面。 |
| Window | setBackgroundColor(color: string, callback: AsyncCallback&lt;void&gt;): void | 设置窗口的背景色。 |
| Window | setBrightness(brightness: number, callback: AsyncCallback&lt;void&gt;): void | 设置屏幕亮度值。 |
| Window | setTouchable(isTouchable: boolean, callback: AsyncCallback&lt;void&gt;): void | 设置窗口是否为可触状态。 |
| Window | moveTo(x: number, y: number, callback: AsyncCallback&lt;void&gt;): void | 移动当前窗口位置。 |
| Window | resetSize(width: number, height: number, callback: AsyncCallback&lt;void&gt;): void | 改变当前窗口大小。 |
| Window | setFullScreen(isFullScreen: boolean, callback: AsyncCallback&lt;void&gt;): void | 设置窗口是否全屏显示。 |
| Window | setLayoutFullScreen(isLayoutFullScreen: boolean, callback: AsyncCallback&lt;void&gt;): void | 设置窗口布局是否为全屏布局。 |
| Window | setSystemBarEnable(names: Array&lt;'status'\|'navigation'&gt;): Promise&lt;void&gt; | 设置导航栏、状态栏是否显示。 |
| Window | setSystemBarProperties(systemBarProperties: SystemBarProperties, callback: AsyncCallback&lt;void&gt;): void | 设置窗口内导航栏、状态栏属性。<br/>`systemBarProperties`:导航栏、状态栏的属性集合。 |
| Window | show(callback: AsyncCallback\<void>): void | 显示当前窗口。 |
| window静态方法 | createWindow(config: Configuration, callback: AsyncCallback<Window>): void | 创建系统窗口。<br/>-`config`:创建窗口时的参数。 |
| Window | setUIContent(path: string, callback: AsyncCallback&lt;void&gt;): void | 为当前窗口加载具体页面。 |
| Window | setWindowBackgroundColor(color: string, callback: AsyncCallback&lt;void&gt;): void | 设置窗口的背景色。 |
| Window | setWindowBrightness(brightness: number, callback: AsyncCallback&lt;void&gt;): void | 设置屏幕亮度值。 |
| Window | setWindowTouchable(isTouchable: boolean, callback: AsyncCallback&lt;void&gt;): void | 设置窗口是否为可触状态。 |
| Window | moveWindowTo(x: number, y: number, callback: AsyncCallback&lt;void&gt;): void | 移动当前窗口位置。 |
| Window | resize(width: number, height: number, callback: AsyncCallback&lt;void&gt;): void | 改变当前窗口大小。 |
| Window | setWindowLayoutFullScreen(isLayoutFullScreen: boolean, callback: AsyncCallback&lt;void&gt;): void | 设置窗口布局是否为全屏布局。 |
| Window | setWindowSystemBarEnable(names: Array&lt;'status'\|'navigation'&gt;): Promise&lt;void&gt; | 设置导航栏、状态栏是否显示。 |
| Window | setWindowSystemBarProperties(systemBarProperties: SystemBarProperties, callback: AsyncCallback&lt;void&gt;): void | 设置窗口内导航栏、状态栏属性。<br/>`systemBarProperties`:导航栏、状态栏的属性集合。 |
| Window | showWindow(callback: AsyncCallback\<void>): void | 显示当前窗口。 |
| Window | on(type: 'touchOutside', callback: Callback&lt;void&gt;): void | 开启本窗口区域外的点击事件的监听。 |
| Window | destroy(callback: AsyncCallback&lt;void&gt;): void | 销毁当前窗口。 |
| Window | destroyWindow(callback: AsyncCallback&lt;void&gt;): void | 销毁当前窗口。 |
## 设置应用主窗口
`Stage`模型下,应用主窗口由`Ability`创建并维护生命周期。在`Ability``onWindowStageCreate`回调中,通过`WindowStage`获取应用主窗口,即可对其进行属性设置等操作
`Stage`模型下,应用主窗口由`Ability`创建并维护生命周期。在`Ability``onWindowStageCreate`回调中,通过`WindowStage`获取应用主窗口,即可对其进行属性设置等操作
### 开发步骤
......@@ -82,7 +81,7 @@ class MainAbility extends Ability {
console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data));
// 2.设置主窗口属性。以设置"是否可触"属性为例。
let isTouchable = true;
windowClass.setTouchable(isTouchable, (err) => {
windowClass.setWindowTouchable(isTouchable, (err) => {
if (err.code) {
console.error('Failed to set the window to be touchable. Cause:' + JSON.stringify(err));
return;
......@@ -117,10 +116,10 @@ class MainAbility extends Ability {
子窗口创建成功后,可以改变其大小、位置等,还可以根据应用需要设置窗口背景色、亮度等属性。
3. 加载显示子窗口的具体内容。
通过`loadContent``show`接口加载显示子窗口的具体内容。
通过`setUIContent``showWindow`接口加载显示子窗口的具体内容。
4. 销毁子窗口。
当不再需要某些子窗口时,可根据具体实现逻辑,使用`destroy`接口销毁子窗口。
当不再需要某些子窗口时,可根据具体实现逻辑,使用`destroyWindow`接口销毁子窗口。
```ts
import Ability from '@ohos.application.Ability'
......@@ -138,14 +137,14 @@ class MainAbility extends Ability {
sub_windowClass = data;
console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data));
// 2.子窗口创建成功后,设置子窗口的位置、大小及相关属性等。
sub_windowClass.moveTo(300, 300, (err) => {
sub_windowClass.moveWindowTo(300, 300, (err) => {
if (err.code) {
console.error('Failed to move the window. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in moving the window.');
});
sub_windowClass.resetSize(500, 1000, (err) => {
sub_windowClass.resize(500, 500, (err) => {
if (err.code) {
console.error('Failed to change the window size. Cause:' + JSON.stringify(err));
return;
......@@ -153,14 +152,14 @@ class MainAbility extends Ability {
console.info('Succeeded in changing the window size.');
});
// 3.为子窗口加载对应的目标页面。
sub_windowClass.loadContent("pages/page3", (err) => {
sub_windowClass.setUIContent("pages/page3", (err) => {
if (err.code) {
console.error('Failed to load the content. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in loading the content.');
// 3.显示子窗口。
sub_windowClass.show((err) => {
sub_windowClass.showWindow((err) => {
if (err.code) {
console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
return;
......@@ -173,7 +172,7 @@ class MainAbility extends Ability {
destroySubWindow() {
// 4.销毁子窗口。当不再需要子窗口时,可根据具体实现逻辑,使用destroy对其进行销毁。
sub_windowClass.destroy((err) => {
sub_windowClass.destroyWindow((err) => {
if (err.code) {
console.error('Failed to destroy the window. Cause: ' + JSON.stringify(err));
return;
......@@ -207,9 +206,8 @@ class MainAbility extends Ability {
通过`getMainWindow`接口获取应用主窗口。
2. 实现沉浸式效果。有以下三种方式:
- 方式一:调用`setFullScreen`接口,设置应用主窗口为全屏显示,此时导航栏、状态栏将隐藏,从而达到沉浸式效果。
- 方式二:调用`setSystemBarEnable`接口,设置导航栏、状态栏不显示,从而达到沉浸式效果。
- 方式三:调用`setLayoutFullScreen`接口,设置应用主窗口为全屏布局;然后调用`setSystemProperties`接口,设置导航栏、状态栏的透明度、背景/文字颜色以及高亮图标等属性,使之保持与主窗口显示协调一致,从而达到沉浸式效果。
- 方式一:调用`setWindowSystemBarEnable`接口,设置导航栏、状态栏不显示,从而达到沉浸式效果。
- 方式二:调用`setWindowLayoutFullScreen`接口,设置应用主窗口为全屏布局;然后调用`setWindowSystemBarProperties`接口,设置导航栏、状态栏的透明度、背景/文字颜色以及高亮图标等属性,使之保持与主窗口显示协调一致,从而达到沉浸式效果。
3. 加载显示沉浸式窗口的具体内容。
通过`loadContent`接口加载沉浸式窗口的具体内容。
......@@ -229,27 +227,18 @@ class MainAbility extends Ability {
windowClass = data;
console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data));
// 2.实现沉浸式效果。方式一:设置应用主窗口为全屏显示。
let isFullScreen = true;
windowClass.setFullScreen(isFullScreen, (err) => {
if (err.code) {
console.error('Failed to enable the full-screen mode. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in enabling the full-screen mode.');
});
// 2.实现沉浸式效果。方式二:设置导航栏、状态栏不显示。
// 2.实现沉浸式效果。方式一:设置导航栏、状态栏不显示。
let names = [];
windowClass.setSystemBarEnable(names, (err) => {
windowClass.setWindowSystemBarEnable(names, (err) => {
if (err.code) {
console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in setting the system bar to be visible.');
});
// 2.实现沉浸式效果。方式:设置窗口为全屏布局,配合设置导航栏、状态栏的透明度、背景/文字颜色及高亮图标等属性,与主窗口显示保持协调一致。
// 2.实现沉浸式效果。方式:设置窗口为全屏布局,配合设置导航栏、状态栏的透明度、背景/文字颜色及高亮图标等属性,与主窗口显示保持协调一致。
let isLayoutFullScreen = true;
windowClass.setLayoutFullScreen(isLayoutFullScreen, (err) => {
windowClass.setWindowLayoutFullScreen(isLayoutFullScreen, (err) => {
if (err.code) {
console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err));
return;
......@@ -263,7 +252,7 @@ class MainAbility extends Ability {
statusBarContentColor: '#ffffff',
navigationBarContentColor: '#ffffff'
};
windowClass.setSystemBarProperties(sysBarProps, (err) => {
windowClass.setWindowSystemBarProperties(sysBarProps, (err) => {
if (err.code) {
console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err));
return;
......@@ -292,9 +281,10 @@ class MainAbility extends Ability {
### 开发步骤
1. 申请权限。
创建`WindowType.TYPE_FLOAT`即悬浮窗类型的窗口,需要在`module.json5`文件的`requestPermissions`对象中配置`ohos.permission.SYSTEM_FLOAT_WINDOW`权限。更多配置信息详见[Stage模型应用程序包结构](../quick-start/application-package-structure-stage.md)
创建`WindowType.TYPE_FLOAT`即悬浮窗类型的窗口,需要在`module.json5`文件的`requestPermissions`对象中配置`ohos.permission.SYSTEM_FLOAT_WINDOW`权限。更多配置信息详见[module.json5配置文件](../quick-start/module-configuration-file.md)
> **说明:**
>
> 虽然悬浮窗具备始终在前台显示的能力,但如果创建悬浮窗的应用任务被系统回收,仍然会导致悬浮窗从界面移除。如果想要保持悬浮窗口始终在前台显示,请申请[长时任务](../task-management/background-task-overview.md)。
```json
......@@ -316,17 +306,17 @@ class MainAbility extends Ability {
```
2. 创建悬浮窗。
通过`window.create`接口创建悬浮窗类型的窗口。
通过`window.createWindow`接口创建悬浮窗类型的窗口。
3. 对悬浮窗进行属性设置等操作。
悬浮窗窗口创建成功后,可以改变其大小、位置等,还可以根据应用需要设置悬浮窗背景色、亮度等属性。
4. 加载显示悬浮窗的具体内容。
通过`loadContent``show`接口加载显示悬浮窗的具体内容。
通过`setUIContent``showWindow`接口加载显示悬浮窗的具体内容。
5. 销毁悬浮窗。
当不再需要悬浮窗时,可根据具体实现逻辑,使用`destroy`接口销毁悬浮窗。
当不再需要悬浮窗时,可根据具体实现逻辑,使用`destroyWindow`接口销毁悬浮窗。
```ts
import Ability from '@ohos.application.Ability'
......@@ -337,7 +327,8 @@ class MainAbility extends Ability {
onWindowStageCreate(windowStage) {
// 2. 创建悬浮窗。
let windowClass = null;
window.create(this.context, "floatWindow", window.WindowType.TYPE_FLOAT, (err, data) => {
let config = {name: "floatWindow", windowType: window.WindowType.TYPE_FLOAT, ctx: this.context};
window.createWindow(config, (err, data) => {
if (err.code) {
console.error('Failed to create the floatWindow. Cause: ' + JSON.stringify(err));
return;
......@@ -345,14 +336,14 @@ class MainAbility extends Ability {
console.info('Succeeded in creating the floatWindow. Data: ' + JSON.stringify(data));
windowClass = data;
// 3.悬浮窗窗口创建成功后,设置悬浮窗的位置、大小及相关属性等。
windowClass.moveTo(300, 300, (err) => {
windowClass.moveWindowTo(300, 300, (err) => {
if (err.code) {
console.error('Failed to move the window. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in moving the window.');
});
windowClass.resetSize(500, 1000, (err) => {
windowClass.resize(500, 500, (err) => {
if (err.code) {
console.error('Failed to change the window size. Cause:' + JSON.stringify(err));
return;
......@@ -360,14 +351,14 @@ class MainAbility extends Ability {
console.info('Succeeded in changing the window size.');
});
// 4.为悬浮窗加载对应的目标页面。
windowClass.loadContent("pages/page4", (err) => {
windowClass.setUIContent("pages/page4", (err) => {
if (err.code) {
console.error('Failed to load the content. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in loading the content.');
// 4.显示悬浮窗。
windowClass.show((err) => {
windowClass.showWindow((err) => {
if (err.code) {
console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
return;
......@@ -376,7 +367,7 @@ class MainAbility extends Ability {
});
});
//5.销毁悬浮窗。当不再需要悬浮窗时,可根据具体实现逻辑,使用destroy对其进行销毁。
windowClass.destroy((err) => {
windowClass.destroyWindow((err) => {
if (err.code) {
console.error('Failed to destroy the window. Cause: ' + JSON.stringify(err));
return;
......
......@@ -15,14 +15,14 @@
| 实例名 | 接口名 | 描述 |
| -------- | -------- | -------- |
| window静态方法 | create(ctx: Context, id: string, type: WindowType, callback: AsyncCallback&lt;Window&gt;): void | 创建窗口。<br/>-`ctx`:为应用上下文信息。当`Context`[ServiceExtensionContext](../reference/apis/js-apis-inner-application-serviceExtensionContext.md)时,创建系统窗口。<br/>-`type`:为创建的窗口类型。 |
| Window | resetSize(width: number, height: number, callback: AsyncCallback&lt;void&gt;): void | 改变当前窗口大小。 |
| Window | moveTo(x: number, y: number, callback: AsyncCallback&lt;void&gt;): void | 移动当前窗口位置。 |
| Window | loadContent(path: string, callback: AsyncCallback&lt;void&gt;): void | 为当前窗口加载具体页面。 |
| Window | show(callback: AsyncCallback\<void>): void | 显示当前窗口。 |
| window静态方法 | createWindow(config: Configuration, callback: AsyncCallback<Window>): void | 创建子窗口或系统窗口。<br/>-`config`:创建窗口时的参数。 |
| Window | resize(width: number, height: number, callback: AsyncCallback&lt;void&gt;): void | 改变当前窗口大小。 |
| Window | moveWindowTo(x: number, y: number, callback: AsyncCallback&lt;void&gt;): void | 移动当前窗口位置。 |
| Window | SetUIContent(path: string, callback: AsyncCallback&lt;void&gt;): void | 为当前窗口加载具体页面。 |
| Window | showWindow(callback: AsyncCallback\<void>): void | 显示当前窗口。 |
| Window | on(type: 'touchOutside', callback: Callback&lt;void&gt;): void | 开启本窗口区域外的点击事件的监听。 |
| Window | hide (callback: AsyncCallback\<void>): void | 隐藏当前窗口。此接口为系统接口。 |
| Window | destroy(callback: AsyncCallback&lt;void&gt;): void | 销毁当前窗口。 |
| Window | destroyWindow(callback: AsyncCallback&lt;void&gt;): void | 销毁当前窗口。 |
## 开发步骤
......@@ -33,7 +33,7 @@
1. 创建系统窗口。
[ServiceExtensionContext](../reference/apis/js-apis-inner-application-serviceExtensionContext.md)下,使用`window.create`接口创建音量条系统窗口。
[ServiceExtensionContext](../reference/apis/js-apis-inner-application-serviceExtensionContext.md)下,使用`window.createWindow`接口创建音量条系统窗口。
2. 操作或设置系统窗口的属性。
......@@ -41,11 +41,11 @@
3. 加载显示系统窗口的具体内容。
通过`loadContent``show`接口加载显示音量条窗口的具体内容。
通过`SetUIContent``showWindow`接口加载显示音量条窗口的具体内容。
4. 隐藏/销毁系统窗口。
当不再需要音量条窗口时,可根据具体实现逻辑,使用`hide`接口或`destroy`接口对其进行隐藏或销毁。
当不再需要音量条窗口时,可根据具体实现逻辑,使用`hide`接口或`destroyWindow`接口对其进行隐藏或销毁。
```ts
import ExtensionContext from '@ohos.application.ServiceExtensionAbility';
......@@ -57,7 +57,8 @@ export default class ServiceExtensionAbility1 extends ExtensionContext {
globalThis.abilityWant = want;
// 1.创建音量条窗口。
let windowClass = null;
window.create(this.context, "volume", window.WindowType.TYPE_VOLUME_OVERLAY, (err, data) => {
let config = {name: "volume", windowType: window.WindowType.TYPE_VOLUME_OVERLAY, ctx: this.context};
window.createWindow(config, (err, data) => {
if (err.code) {
console.error('Failed to create the volume window. Cause:' + JSON.stringify(err));
return;
......@@ -65,14 +66,14 @@ export default class ServiceExtensionAbility1 extends ExtensionContext {
console.info('Succeeded in creating the volume window.')
windowClass = data;
// 2.创建音量条窗口成功之后,可以改变其大小、位置或设置背景色、亮度等属性。
windowClass.moveTo(300, 300, (err) => {
windowClass.moveWindowTo(300, 300, (err) => {
if (err.code) {
console.error('Failed to move the window. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in moving the window.');
});
windowClass.resetSize(500, 1000, (err) => {
windowClass.resize(500, 500, (err) => {
if (err.code) {
console.error('Failed to change the window size. Cause:' + JSON.stringify(err));
return;
......@@ -80,14 +81,14 @@ export default class ServiceExtensionAbility1 extends ExtensionContext {
console.info('Succeeded in changing the window size.');
});
// 3.为音量条窗口加载对应的目标页面。
windowClass.loadContent("pages/page_volume", (err) => {
windowClass.setUIContent("pages/page_volume", (err) => {
if (err.code) {
console.error('Failed to load the content. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in loading the content.');
// 3.显示音量条窗口。
windowClass.show((err) => {
windowClass.showWindow((err) => {
if (err.code) {
console.error('Failed to show the window. Cause:' + JSON.stringify(err));
return;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册