HarmonyOS中通过createSubWindow方法来创建子窗口,子窗口在创建时,可以通过moveWindowTo方法来确定子窗口的初始位置,此方法用于移动窗口位置,子窗口创建之后即可以使用window.findWindow来查找name所对应的窗口。
// Until.ets封装的子窗口类 import { common } from '@kit.AbilityKit'; import { window } from '@kit.ArkUI'; import { hilog } from '@kit.PerformanceAnalysisKit'; export class Loading { win: window.Window | undefined = undefined; OpenSubWindows(UIContext: UIContext) { (UIContext?.getHostContext() as common.UIAbilityContext).windowStage.createSubWindow('FloatPage', (err, windowClass) => { if (err.code > 0) { hilog.error(0x0000, 'testTag', '%{public}s', `failed to create subWindow Cause: ${err.message}`); return; } this.win = windowClass; try { // 设置子窗口加载页 windowClass.setUIContent('pages/FloatPage', () => { windowClass.setWindowBackgroundColor('#00000000'); }); // 设置子窗口左上角坐标 windowClass.moveWindowTo(0, 2285); // 设置子窗口大小 windowClass.resize(1260, 200); // 展示子窗口 windowClass.showWindow(); } catch (err) { hilog.error(0x0000, 'testTag', '%{public}s', `failed to create subWindow Cause:${err}`); } }); } CloseSubWindows() { this.win?.destroyWindow(); } } export default new Loading();
// PageA.ets子页面 import { window } from '@kit.ArkUI'; import Loading from './Until'; @Entry @Component struct PageA { floatWindow: window.Window = window.findWindow('FloatPage'); aboutToAppear(): void { this.floatWindow.moveWindowTo(0, 2500); } build() { Column() .width('100%') .height('100%') .backgroundColor('#5291FF') .onClick(() => { this.getUIContext().getRouter().back(); Loading.CloseSubWindows(); }); } }
// SharedModalAcrossPages.ets主页面 import Loading from './Until'; @Entry @Component struct SharedModalAcrossPages { @State currentIndex: number = 0; @State selectedIndex: number = 0; private controller: TabsController = new TabsController(); aboutToAppear(): void { Loading.OpenSubWindows(this.getUIContext()); } build() { Column() { Tabs({ barPosition: BarPosition.End, index: this.currentIndex, controller: this.controller }) { TabContent() { Column() .width('100%') .height('100%') .backgroundColor('#00CB87') .onClick(() => { Loading.OpenSubWindows(this.getUIContext()); }) .onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => { if (isVisible && currentRatio >= 1.0) { Loading.OpenSubWindows(this.getUIContext()); } if (!isVisible && currentRatio <= 0.0) { console.info('新闻页隐藏了'); } }); }.tabBar('内容0'); TabContent() { Column().width('100%').height('100%').backgroundColor('#007DFF'); }.tabBar('内容1'); TabContent() { Column().width('100%').height('100%').backgroundColor('#FFBF00'); }.tabBar('内容2'); TabContent() { Column().width('100%').height('100%').backgroundColor('#E67C92'); }.tabBar('内容3'); } .vertical(false) .barMode(BarMode.Fixed) .barHeight(56) .animationDuration(400) .onChange((index: number) => { // currentIndex控制TabContent显示页签 this.currentIndex = index; this.selectedIndex = index; }) .onAnimationStart((index: number, targetIndex: number) => { if (index === targetIndex) { return; } // selectedIndex控制自定义TabBar内Image和Text颜色切换 this.selectedIndex = targetIndex; }) .height('100%') .backgroundColor('#F1F3F5'); }.width('100%'); } }
// FloatPage.ets子窗口页面 import { common } from '@kit.AbilityKit'; import Loading from './Until'; @Entry @Component struct FloatPage { private message: string = '浮窗页面'; private context = this.getUIContext().getHostContext() as common.UIAbilityContext; onBackPress() { Loading.CloseSubWindows(); return true; } build() { Row() { Text(this.message) .fontSize(15) .fontWeight(FontWeight.Bold) .margin({ left: '16vp' }); Button('跳转新页面') .onClick(() => { const mainR = this.context.windowStage.getMainWindowSync() .getUIContext() .getRouter(); mainR.pushUrl({ url: 'pages/PageA' }); }); Blank(); Button('关闭子窗口') .onClick(() => { Loading.CloseSubWindows(); }) .margin({ right: '16vp' }); } .width('100%') .height(50) .backgroundColor('#FFFFFF'); } }