HarmonyOS开发者版主1|
ArkUI (方舟UI框架)
UI
HarmonyOS 5
HarmonyOS HAR包中使用window.createWindow添加一个应用子窗进去,实现一个应用内的悬浮球功能(API12+)
应用子窗的问题,想在HAR包中使用window.createWindow塞一个应用子窗进去,实现一个应用内的悬浮球功能。
现在遇到了两个问题, 一个是HAR不支持pages只能通过路由跳转,有没有办法能直接打开子窗页面?直接这样用会报1300002 窗口状态异常,必须得先setUIContent一个页面再路由跳转才行;另一个是怎么样在HAR里写逻辑通过手势拖动这个子窗的位置?
1
浏览73 发布于2026-04-02 11:25未知归属地
全部评论
最多点赞
最新发布
最早发布

参考Demo:
收起
自动换行
深色代码主题
复制
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
import SubWindowManager from './SubWindowManager';
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
SubWindowManager?.registerAbilityLifecycleCallback(this.context)
}
onDestroy(): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
}
onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
AppStorage.setOrCreate('windowStage', windowStage)
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
return;
}
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
});
}
onWindowStageDestroy(): void {
// Main window is destroyed, release UI related resources
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
}
onForeground(): void {
// Ability has brought to foreground
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
}
onBackground(): void {
// Ability has back to background
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
}
}
HAR包创建:
收起
自动换行
深色代码主题
复制
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
import * as Index from '../components/SubWindow';
export class Test {
private static instance: Test = new Test()
public static getInstance() {
return Test.instance
}
public showSubWindow() {
let mWindowStage = AppStorage.get('windowStage') as window.WindowStage
// 1.创建应用子窗口。
if (mWindowStage == null) {
console.error('Failed to create the subwindow. Cause: windowStage_ is null');
} else {
mWindowStage.createSubWindow("mySubWindow", (err: BusinessError, data) => {
let errCode: number = err.code;
if (errCode) {
console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(err));
return;
}
let sub_windowClass = data;
console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data));
// 2.子窗口创建成功后,设置子窗口的位置、大小及相关属性等。
sub_windowClass.moveWindowTo(300, 300, (err: BusinessError) => {
let errCode: number = err.code;
if (errCode) {
console.error('Failed to move the window. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in moving the window.');
});
sub_windowClass.resize(500, 500, (err: BusinessError) => {
let errCode: number = err.code;
if (errCode) {
console.error('Failed to change the window size. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in changing the window size.');
});
// 3.为子窗口加载对应的目标页面。
sub_windowClass.loadContentByName(Index.entryName, (err: BusinessError) => {
let errCode: number = err.code;
if (errCode) {
console.error('Failed to load the content. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in loading the content.');
// 3.显示子窗口。
(sub_windowClass as window.Window).showWindow((err: BusinessError) => {
let errCode: number = err.code;
if (errCode) {
console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in showing the window.');
});
});
})
}
}
}
librarby的Index:
收起
自动换行
深色代码主题
复制
export { Test } from './src/main/ets/components/Test'
问题2:在手势监听中获取获取坐标,然后通过moveWindowTo移动。
展开全部 
1楼回复于2026-04-02 11:25 未知归属地
写回答
所见即所得编辑器, editor1
工具栏 撤消 快捷键 Ctrl+Z 重做 快捷键 Ctrl+Y 格式刷 快捷键 Ctrl+Shift+C 清除格式 加粗 快捷键 Ctrl+B 倾斜 快捷键 Ctrl+I 下划线 快捷键 Ctrl+U 删除线格式调整字号 文本颜色 背景颜色 增加缩进量 减少缩进量 左对齐 居中 右对齐 两端对齐段落间距行间距 项目列表 编号列表 插入水平线 块引用 插入/编辑超链接 快捷键 Ctrl+K 取消超链接 图片 表格 上传附件 导入Word 插入代码 MD编辑器 全屏模式
0/50000
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要提问题
相关推荐
换一批
1
HarmonyOS window.createWindow设置属性SYSTEM_FLOAT_WINDOW(API12+)
2
HarmonyOS 是否支持关掉某个har包的全部日志? (API12+)
3
HarmonyOS HAR包内无法使用router.pushUrl进行跳转如何解决(API12+)
4
HarmonyOS 调用createWindow方法时报错1300002, This window state is abnormal.(API12+)
5
HarmonyOS 开发应用使用悬浮窗权限,编译报错1300002 (API12+)
6
HarmonyOS 如何实现悬浮窗功能 (API12+)
7
HarmonyOS 如何创建一个悬浮子窗口,在主窗口最小化后还能显示在屏幕上,并在其他应用上 (API12+)
8
HarmonyOS 如何创建一个跨页面和App的悬浮窗? (API12+)
了解社区公约,与您携手共创和谐专业的开发者社区。




































苏公安网备 32011402010933号