#鸿蒙通关秘籍#如何在HarmonyOS NEXT中实现悬浮窗的创建与自定义样式?

HarmonyOS
2024-12-05 14:27:18
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
雪域狼CLI

在HarmonyOS NEXT中,实现悬浮窗的创建与自定义样式可以通过创建子窗口,并根据实际需求调整窗口的布局与外观。首先,在EntryAbility中获取WindowStage,可以利用windowStage.createSubWindow("mySubWindow")来创建子窗口。

onWindowStageCreate(windowStage: window.WindowStage): void {
  windowStage.loadContent('pages/Page', (err, data) => {
    if (err.code) {
      console.error('Failed to load the content due to:', JSON.stringify(err));
      return;
    }
    AppStorage.setOrCreate("windowStage", windowStage);
  });
}

// 创建子窗口并设置样式
this.windowStage.createSubWindow("mySubWindow", (err, windowClass) => {
  if (err.code > 0) {
    console.error("Failed to create subWindow due to:" + err.message);
    return;
  }
  try {
    windowClass.setUIContent("pages/MySubWindow", () => {
      windowClass.setWindowBackgroundColor("#00000000");
    });
    windowClass.moveWindowTo(0, 200);
    windowClass.resize(vp2px(75), vp2px(75));
    windowClass.showWindow();
    windowClass.setWindowLayoutFullScreen(true);
  } catch (err) {
    console.error("Failed to create subWindow due to:" + err);
  }
});
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
分享
微博
QQ
微信
回复
2024-12-05 17:03:06
相关问题