HarmonyOS 自定义dialog样式的时候,dialog弹出之后,底部总有一个空白区域

HarmonyOS 自定义dialog样式的时候,dialog弹出之后,底部总有一个空白区域。

HarmonyOS
2024-10-08 10:19:15
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

思路如下:

1、根据window.on("keyboardHeightChange")获取软键盘系统高度。

2、在CustomDialog的根组件下通过onAreaChange确认Dialog实际抬升高度。

3、两数相减得到具体偏移量。样例代码如下:

import { inputMethod } from '@kit.IMEKit'  
import { window } from '@kit.ArkUI'  
@CustomDialog  
struct CustomDialogExample {  
  controller?: CustomDialogController  
  @State @Watch('keyboardHeightChange') keyboardHeight: number = 0  
  @State @Watch('keyboardHeightChange') realKeyboardHeight: number = 0  
  @State offsetY: number = 0  
  aboutToAppear(): void {  
    window.getLastWindow(getContext(this), (err, window) => {  
      window.on("keyboardHeightChange", (keyboardHeight) => {  
        if (keyboardHeight !== 0) {  
          this.keyboardHeight = px2vp(keyboardHeight)  
        }  
      });  
    })  
  }  
  keyboardHeightChange() {  
    if (this.keyboardHeight !== 0 && this.realKeyboardHeight !== 0) {  
      this.offsetY = this.realKeyboardHeight - this.keyboardHeight  
    }  
  }  
  build() {  
    Column() {  
      Text('content')  
        .fontSize(20)  
      TextArea()  
        .width("100%")  
        .height(100)  
      Text('content')  
        .fontSize(20)  
    }  
    .width("100%")  
    .offset({y: this.offsetY})  
    .backgroundColor(Color.White)  
    .borderRadius({topLeft: 32, topRight: 32})  
    .onAreaChange((oldValue: Area, newValue: Area) => {  
      if (Number(oldValue.globalPosition.y) > Number(newValue.globalPosition.y)) {  
        this.realKeyboardHeight = Number(oldValue.globalPosition.y) - Number(newValue.globalPosition.y)  
        console.log("")  
      }  
    })  
  }  
}  
@Entry  
@Component  
struct CustomDialogTest {  
  @State message: string = 'Hello World';  
  dialogController: CustomDialogController = new CustomDialogController({  
    builder: CustomDialogExample(),  
    alignment: DialogAlignment.Bottom,  
    customStyle: true  
  })  
  build() {  
    RelativeContainer() {  
      Text(this.message)  
        .id('HelloWorld')  
        .fontSize(50)  
        .fontWeight(FontWeight.Bold)  
        .alignRules({  
          center: { anchor: 'container', align: VerticalAlign.Center },  
          middle: { anchor: 'container', align: HorizontalAlign.Center }  
        })  
        .onClick(() => {  
          this.dialogController.open()  
        })  
    }  
    .height('100%')  
    .width('100%')  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
分享
微博
QQ
微信
回复
2024-10-08 15:52:52
相关问题
如何在全局实现一个自定义dialog弹窗
3637浏览 • 1回复 待解决
如何封装一个自定义Dialog对话框
3008浏览 • 1回复 待解决
HarmonyOS 自定义Dialog宽度
838浏览 • 1回复 待解决
HarmonyOS 自定义全局dialog
681浏览 • 1回复 待解决
HarmonyOS 自定义全屏dialog
875浏览 • 1回复 待解决
HarmonyOS 如何封装自定义Dialog
804浏览 • 1回复 待解决
HarmonyOS 自定义dialog open无效
947浏览 • 1回复 待解决
HarmonyOS 自定义dialog相关问题
732浏览 • 1回复 待解决
HarmonyOS 自定义Dialog显示问题
1237浏览 • 1回复 待解决
HarmonyOS 用CustomDialog自定义Dialog
1122浏览 • 1回复 待解决
HarmonyOS 自定义Dialog高度问题
749浏览 • 1回复 待解决