HarmonyOS鸿蒙Next中如何打开透明背景的enrty组件

发布于 1周前 作者 wuwangju 来自 鸿蒙OS

HarmonyOS鸿蒙Next中如何打开透明背景的enrty组件 如何打开透明背景的enrty组件

3 回复

您可以使用setwindowbackgroundcolor

參考如下: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5#setwindowbackgroundcolor9

router路由模式请参考下面demo:

// Page1.ets

import window from '@ohos.window';

@Entry
@Component
struct Page2 {
  @State message: string = 'page Page2';

  onPageHide() {
    console.log("pageHide")
  }

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Button("pageB").onClick(() => {
          let windowStege: window.WindowStage = AppStorage.get("windowStage") as window.WindowStage;
          windowStege.createSubWindow("hello", (err, win) => {
            win.setUIContent('pages/Page2');
            win.showWindow();
          })
        })
      }
      .width('100%')
    }
    .height('100%')
    .backgroundColor(Color.Pink)
  }
}
// Page2.ets

import window from '@ohos.window';

@Entry
@Component
struct Index {
  @State message: string = 'page Index';

  aboutToAppear() {
    window.findWindow("hello").setWindowBackgroundColor("#00000000")
  }

  onBackPress() {
    window.findWindow("hello").destroyWindow().then(res => {
      console.log("destroyWindow success")
    }).catch(() => {
      console.log("destroyWindow fail")
    })
    return true
  }

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
      .backgroundColor("#00000000")
    }
    .alignItems(VerticalAlign.Top)
    .height('100%')
    .backgroundColor("#80ffffff")
  }
}

注意需要在EntryAbility里面添加关键代码那一行。

onWindowStageCreate(windowStage: window.WindowStage)
: void {
  // Main window is created, set main page for this ability
  hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

  windowStage.loadContent(
    'pages/Page231206095213071',
    (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      AppStorage.setAndLink("windowStage", windowStage); // 关键代码
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    }
  );
}

NavDestination支持Dialog类型页面:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-navdestination-V5#navdestinationmode 枚举说明-11

NavDestinationMode.STANDARD: 标准类型
NavDestinationMode.DIALOG: 默认透明。

win.setUIContent无法传参,该接口需要在loadContent()或setUIContent()调用生效后使用。

这段代码必须要加上才能返回,传参通过LocalStorage传递状态属性给加载的页面

onBackPress() {
  window.findWindow("WindowBackgroundColor").destroyWindow().then(res => {
    console.log("destroyWindow success")
  }).catch(() => {
    console.log("destroyWindow fail")
  })
  return true
}

更多关于HarmonyOS鸿蒙Next中如何打开透明背景的enrty组件的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,打开透明背景的entry组件可以通过设置AbilitySlice的样式来实现。具体步骤如下:

  1. 创建AbilitySlice:首先,在MainAbility中创建一个AbilitySlice,并在onStart方法中设置该AbilitySlice为当前显示的页面。

  2. 设置透明背景:在AbilitySlice的布局文件中,设置根布局的背景为透明。可以使用ohos:background_element属性,将其值设置为透明颜色或透明图片。

  3. 修改config.json:在config.json文件中,找到对应的ability配置项,将"supportWindowMode": "full_screen"改为"supportWindowMode": "floating",以确保页面支持浮动窗口模式。

  4. 设置窗口属性:在AbilitySliceonStart方法中,通过Window对象设置窗口的透明度和背景。可以使用Window.setTransparent(true)Window.setBackgroundColor(Color.TRANSPARENT)来实现。

  5. 测试运行:编译并运行应用,查看entry组件是否显示为透明背景。

通过以上步骤,可以在HarmonyOS鸿蒙Next中实现透明背景的entry组件。

在HarmonyOS鸿蒙Next中,打开透明背景的entry组件可以通过设置Componentbackground_element属性来实现。具体步骤如下:

  1. 创建entry组件:在布局文件中定义entry组件。
  2. 设置透明背景:在代码中通过setBackgroundElement方法将背景设置为透明。例如:
    entry.setBackgroundElement(new Element().setColor(Color.TRANSPARENT));

这样,entry组件的背景就会变为透明。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!