HarmonyOS鸿蒙Next中Component和win如何设置背景色透明

HarmonyOS鸿蒙Next中Component和win如何设置背景色透明 在主window中通过createSubWindow的方式创建了一个window,想设置它的背景色为透明,同时loadContent加载的页面组件也期望设置透明色。是否有api进行设置

3 回复

子窗口设置透明背景色Demo:

Button("Click").onClick(() => {
  let windowStage = AppStorage.get("windowStage") as window.WindowStage;
  windowStage.createSubWindow("test", (err, win) => {
    win.setUIContent("pages/Page", () => {
      // 调整窗口背景透明度 制式ARGB 
      win.setWindowBackgroundColor("#00000000")
      win.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR)
    });
    win.showWindow();
  })
})

参考文档:

showWindow

destroyWindow

更多关于HarmonyOS鸿蒙Next中Component和win如何设置背景色透明的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,设置ComponentWindow的背景色透明可以通过以下方式实现:

  1. 设置Component背景色透明

    • 在ArkUI中,可以通过backgroundColor属性设置组件的背景色。要设置背景色为透明,可以使用Color.Transparent
    [@Component](/user/Component)
    struct MyComponent {
        build() {
            Column() {
                // 组件内容
            }
            .backgroundColor(Color.Transparent)
        }
    }
    
  2. 设置Window背景色透明

    • Window中,可以通过WindowStagesetWindowBackgroundColor方法来设置窗口背景色为透明。
    import window from '[@ohos](/user/ohos).window';
    
    let windowClass = window.getTopWindow();
    windowClass.then(window => {
        window.setWindowBackgroundColor(Color.Transparent);
    });
    

在HarmonyOS鸿蒙Next中,设置Component或Window的背景色透明,可以通过以下方式实现:

  1. Component透明背景:

    • 使用setBackgroundColor方法,将颜色设置为Color.TRANSPARENT
    component.setBackgroundColor(Color.TRANSPARENT);
    
  2. Window透明背景:

    • WindowStyle中设置android:windowBackground为透明色。
    <style name="TransparentWindow" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>
    

确保在布局文件中应用相应的样式或直接在代码中设置。

回到顶部