找回密码
 立即注册
首页 业界区 业界 HarmonyOS NEXT开发教程:全局悬浮窗

HarmonyOS NEXT开发教程:全局悬浮窗

后雪闵 前天 10:26
今天跟大家分享一下HarmonyOS开发中的悬浮窗。
1.png

对于悬浮窗,可能有的同学会想到使用层叠布局是否可以实现,将悬浮窗叠在导航栏组件Tabs上,像这样:
  1. Stack({alignContent:Alignment.BottomEnd}){
  2.   Tabs({barPosition:BarPosition.End}){
  3.     TabContent(){
  4.       Text('page1')
  5.         .fontColor(Color.Black)
  6.         .fontSize(40)
  7.     }
  8.     .tabBar(this.Tabbar())
  9.     TabContent(){
  10.       Page2()
  11.     }
  12.     .tabBar(this.Tabbar())
  13.   
  14.   }
  15.   Row(){
  16.    //这是悬浮窗
  17.   }
  18.   .width(60)
  19.   .height(60)
  20.   .borderRadius(30)
  21.   .backgroundColor(Color.Blue)
  22. }
复制代码
这样的布局在push到下一个页面时悬浮窗就会消失,所以是行不通的。
对于悬浮窗鸿蒙系统有专属的创建方法,就是使用createSubWindow创建子窗口,这个子窗口会悬浮在整个应用上方。具体实现代码如下:
  1. windowStage.createSubWindow('floatWindow',(err: BusinessError, data) =>})
复制代码
在创建完成后,可以设置悬浮窗的尺寸、位置和内容等属性,要注意这里的单位是px,且只能传正整数类型:
  1. //尺寸data.resize(300,300,(err: BusinessError) =>})
复制代码
  1. //位置data.moveWindowTo(400,400,(err: BusinessError) =>})
复制代码
  1. //展示data.showWindow((err: BusinessError) =>});
复制代码
有的时候你可能需要设置悬浮窗是圆形,办法是先去对应的内容页面设置圆角后发现还是会有白色的背景,然后把窗口背景色设置为透明,这个方法最好在setUIContent的回调中设置:
  1. data?.setWindowBackgroundColor('#00000000');
复制代码
当需要关闭悬浮窗时,可以调用destroyWindow方法进行销毁:
  1. window.findWindow(FloatUntil.windowName).destroyWindow()
复制代码
以上就是悬浮窗的常见使用方法,为了便于使用,我把以上方法进行了简单的封装,可以一行代码就实现悬浮窗的展示、移动、关闭等操作:
  1. import EntryAbility from "../entryability/EntryAbility";
  2. import { BusinessError } from "@kit.BasicServicesKit";
  3. import { display, window } from "@kit.ArkUI";
  4. export class FloatUntil{
  5.   static  screen_width = display.getDefaultDisplaySync().width
  6.   static screen_height = display.getDefaultDisplaySync().height
  7.   static float_size = 420
  8.   static  float_positon_x = FloatUntil.screen_width - FloatUntil.float_size - 40
  9.   static  float_positon_y = FloatUntil.screen_height - FloatUntil.float_size - 440
  10.   static  windowName = 'floatWindow'
  11. static  creatAndShowSubWindow(){
  12.     EntryAbility.gloabalWindowStage.createSubWindow(FloatUntil.windowName, (err: BusinessError, data) => {
  13.       let errCode: number = err.code;
  14.       if (errCode) {
  15.         return;
  16.       }
  17.       //位置、尺寸单位是px,只支持正整数
  18.       data.moveWindowTo(FloatUntil.float_positon_x, FloatUntil.float_positon_y, (err: BusinessError) => {
  19.         let errCode: number = err.code;
  20.         if (errCode) {
  21.           return;
  22.         }
  23.         console.info('Succeeded in moving the window.');
  24.       });
  25.       data.resize(FloatUntil.float_size, FloatUntil.float_size, (err: BusinessError) => {
  26.         let errCode: number = err.code;
  27.         if (errCode) {
  28.           return;
  29.         }
  30.       });
  31.       //悬浮窗是否可触
  32.       data.setWindowTouchable(true);
  33.       data.setUIContent("pages/FloatWindow", (err: BusinessError) => {
  34.         let errCode: number = err.code;
  35.         if (errCode) {
  36.           console.error('Failed to load the content. Cause:' + JSON.stringify(err));
  37.           return;
  38.         }
  39.         console.info('Succeeded in loading the content.');
  40.         //设置窗口背景色透明,只有在这调用才不会报错
  41.         data?.setWindowBackgroundColor('#00000000');
  42.         data.showWindow((err: BusinessError) => {
  43.           let errCode: number = err.code;
  44.           if (errCode) {
  45.             console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
  46.             return;
  47.           }
  48.           console.info('Succeeded in showing the window.');
  49.         });
  50.       });
  51.     })
  52.   }
  53.   static moveWindowTo(x:number,y:number){
  54.     window.findWindow(FloatUntil.windowName).moveWindowTo(x,y)
  55.   }
  56.   static destroyFloatWindow(){
  57.      window.findWindow(FloatUntil.windowName).destroyWindow()
  58.   }
  59. }
复制代码
#HarmonyOS语言##ArkTs##工具效率#

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册