最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

HarmonyOS NEXT開發(fā)教程:全局懸浮窗

標(biāo)簽:
HarmonyOS

今天跟大家分享一下HarmonyOS开发中的悬浮窗。

https://img1.sycdn.imooc.com/fd2f9f680853cbcf08201770.jpg
对于悬浮窗,可能有的同学会想到使用层叠布局是否可以实现,将悬浮窗叠在导航栏组件Tabs上,像这样:

Stack({alignContent:Alignment.BottomEnd}){
  Tabs({barPosition:BarPosition.End}){
    TabContent(){
      Text('page1')
        .fontColor(Color.Black)
        .fontSize(40)
    }
    .tabBar(this.Tabbar())
    TabContent(){
      Page2()
    }
    .tabBar(this.Tabbar())
   
  }

  Row(){
	//这是悬浮窗
  }
  .width(60)
  .height(60)
  .borderRadius(30)
  .backgroundColor(Color.Blue)
}

这样的布局在push到下一个页面时悬浮窗就会消失,所以是行不通的。
对于悬浮窗鸿蒙系统有专属的创建方法,就是使用createSubWindow创建子窗口,这个子窗口会悬浮在整个应用上方。具体实现代码如下:

windowStage.createSubWindow('floatWindow',(err: BusinessError, data) => {
})1.2.3.

在创建完成后,可以设置悬浮窗的尺寸、位置和内容等属性,要注意这里的单位是px,且只能传正整数类型:

//尺寸
data.resize(300,300,(err: BusinessError) => {
})
//位置
data.moveWindowTo(400,400,(err: BusinessError) => {
})
//内容
data.setUIContent("pages/FloatWindow", (err: BusinessError) => {
})
//展示
data.showWindow((err: BusinessError) => {
});

有的时候你可能需要设置悬浮窗是圆形,办法是先去对应的内容页面设置圆角后发现还是会有白色的背景,然后把窗口背景色设置为透明,这个方法最好在setUIContent的回调中设置:

data?.setWindowBackgroundColor('#00000000');

当需要关闭悬浮窗时,可以调用destroyWindow方法进行销毁:

window.findWindow(FloatUntil.windowName).destroyWindow()

以上就是悬浮窗的常见使用方法,为了便于使用,我把以上方法进行了简单的封装,可以一行代码就实现悬浮窗的展示、移动、关闭等操作:

import EntryAbility from "../entryability/EntryAbility";
import { BusinessError } from "@kit.BasicServicesKit";
import { display, window } from "@kit.ArkUI";

export class FloatUntil{

  static  screen_width = display.getDefaultDisplaySync().width
  static screen_height = display.getDefaultDisplaySync().height
  static float_size = 420
  static  float_positon_x = FloatUntil.screen_width - FloatUntil.float_size - 40
  static  float_positon_y = FloatUntil.screen_height - FloatUntil.float_size - 440
  static  windowName = 'floatWindow'

 static  creatAndShowSubWindow(){
    EntryAbility.gloabalWindowStage.createSubWindow(FloatUntil.windowName, (err: BusinessError, data) => {
      let errCode: number = err.code;
      if (errCode) {
        return;
      }
      //位置、尺寸单位是px,只支持正整数
      data.moveWindowTo(FloatUntil.float_positon_x, FloatUntil.float_positon_y, (err: BusinessError) => {
        let errCode: number = err.code;
        if (errCode) {
          return;
        }
        console.info('Succeeded in moving the window.');
      });

      data.resize(FloatUntil.float_size, FloatUntil.float_size, (err: BusinessError) => {
        let errCode: number = err.code;
        if (errCode) {
          return;
        }
      });
      //悬浮窗是否可触
      data.setWindowTouchable(true);

      data.setUIContent("pages/FloatWindow", (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.');

        //设置窗口背景色透明,只有在这调用才不会报错
        data?.setWindowBackgroundColor('#00000000');

        data.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.');
        });
      });
    })
  }

  static moveWindowTo(x:number,y:number){
    window.findWindow(FloatUntil.windowName).moveWindowTo(x,y)
  }
  static destroyFloatWindow(){
     window.findWindow(FloatUntil.windowName).destroyWindow()
  }

}

##HarmonyOS语言##ArkTS##工具效率#
點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消