使用View的bindPopup方法显示PopupWindow,当设置内容高度为100%或者高度过高一点时(无法设置满屏),PopupWindow会跑到屏幕的上面去,这个问题在HarmonyOS 鸿蒙Next上如何解决?

使用View的bindPopup方法显示PopupWindow,当设置内容高度为100%或者高度过高一点时(无法设置满屏),PopupWindow会跑到屏幕的上面去,这个问题在HarmonyOS 鸿蒙Next上如何解决?

  1. 问题相关文档
    https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/ts-universal-attributes-popup-V13#bindpopup

  2. 问题描述
    popup内容高度不能设置过高,过高会导致内容偏移严重,如截图,哪怕设置高度是anchorView底部的高度,也会发生偏移

  1. 代码
.bindPopup(this.currentFilterName == item.typeName, {
  builder: this.buildPopupView(index),
  placement: Placement.Bottom,
  targetSpace: 0,
  width: "100%",
  radius: 0,
  popupColor: "#00000000",
  shadow: { radius: 0 },
  // mask: {$r('app.color.black_60')},
  enableArrow: false,
  backgroundBlurStyle: BlurStyle.NONE,
  onStateChange: (visible) => {
    if (!visible.isVisible) {
      this.currentFilterName = ""
    }
  }
})

内容代码:

@Builder
private buildPopupView(index: number) {
  Column() {
    Column() {
      Text("我是一个PopupWindow")
    }
    .width("100%")
    .height(150)
    .backgroundColor($r('app.color.white'))
    .alignItems(HorizontalAlign.Center)
    .justifyContent(FlexAlign.Center)
  }
  .width("100%")
  .height("100%")
  .backgroundColor($r('app.color.black_60'))
  .alignItems(HorizontalAlign.Start)
  .justifyContent(FlexAlign.Start)
}

更多关于使用View的bindPopup方法显示PopupWindow,当设置内容高度为100%或者高度过高一点时(无法设置满屏),PopupWindow会跑到屏幕的上面去,这个问题在HarmonyOS 鸿蒙Next上如何解决?的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于使用View的bindPopup方法显示PopupWindow,当设置内容高度为100%或者高度过高一点时(无法设置满屏),PopupWindow会跑到屏幕的上面去,这个问题在HarmonyOS 鸿蒙Next上如何解决?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS中,ViewbindPopup方法用于绑定并显示PopupWindow。当设置内容高度为100%或过高时,PopupWindow可能会被推到屏幕上方,导致无法满屏显示。这是由于PopupWindow的默认布局行为导致的。

要解决这个问题,可以通过以下步骤进行调整:

  1. 设置PopupWindow的布局参数:在创建PopupWindow时,明确设置其布局参数,确保其高度能够正确适应屏幕。

  2. 使用WindowManager.LayoutParams:通过WindowManager.LayoutParams来手动设置PopupWindow的尺寸和位置,确保其能够占满整个屏幕。

  3. 调整PopupWindow的显示位置:在显示PopupWindow时,使用showAtLocation方法,并指定其显示位置为屏幕的底部。

  4. 处理PopupWindow的内容高度:确保PopupWindow的内容视图能够正确计算和设置高度,避免内容高度超出屏幕范围。

通过以上步骤,可以有效解决PopupWindow在HarmonyOS中无法满屏显示的问题。

回到顶部