HarmonyOS如何将自定义弹窗显示在某个控件的位置附近

HarmonyOS如何将自定义弹窗显示在某个控件的位置附近

HarmonyOS
2024-08-10 11:26:45
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

你可以修改offset和alignment这俩属性的值实现。

@Entry 
@Component 
struct CustomDialogUser { 
  dialogController: CustomDialogController = new CustomDialogController({ 
    builder: CustomDialogExample({ 
      cancel: ()=> { this.onCancel() }, 
      confirm: ()=> { this.onAccept() }, 
    }), 
    alignment: DialogAlignment.TopEnd, 
    offset: { dx: 0, dy: 10 }, 
    gridCount: 4, 
    customStyle: false, 
    backgroundColor: 0xd9ffffff, 
    cornerRadius: 10, 
  }) 
  onCancel() { 
    console.info('Callback when the first button is clicked') 
  } 
  onAccept() { 
    console.info('Callback when the second button is clicked') 
  } 
  build() { 
    Column() { 
      Button('click me') 
        .onClick(() => { 
          this.dialogController.open() 
        }) 
    }.width('100%').margin({ top: 5 }) 
  } 
} 
@CustomDialog 
struct CustomDialogExample { 
  cancel?: () => void 
  confirm?: () => void 
  controller: CustomDialogController 
 
  build() { 
    Column() { 
      Text('我是内容').fontSize(20).margin({ top: 10, bottom: 10 }) 
      Flex({ justifyContent: FlexAlign.SpaceAround }) { 
        Button('cancel') 
          .onClick(() => { 
            this.controller.close() 
            if (this.cancel) { 
              this.cancel() 
            } 
          }).backgroundColor(0xffffff).fontColor(Color.Black) 
        Button('confirm') 
          .onClick(() => { 
            this.controller.close() 
            if (this.confirm) { 
              this.confirm() 
            } 
          }).backgroundColor(0xffffff).fontColor(Color.Red) 
      }.margin({ bottom: 10 }) 
    }.backgroundColor(Color.Red) 
  } 
}

给目标组件调用onAreaChange这个方法,可以获取坐标。这是API文档的示例代码。参考一下。

// xxx.ets 
@Entry 
@Component 
struct AreaExample { 
  @State value: string = 'Text' 
  @State sizeValue: string = '' 
  build() { 
    Column() { 
      Text(this.value) 
        .backgroundColor(Color.Green).margin(30).fontSize(20) 
        .onClick(() => { 
          this.value = this.value + 'Text' 
        }) 
        .onAreaChange((oldValue: Area, newValue: Area) => { 
          console.info(`Ace: on area change, oldValue is ${JSON.stringify(oldValue)} value is ${JSON.stringify(newValue)}`) 
          this.sizeValue = JSON.stringify(newValue) 
        }) 
      Text('new area is: \n' + this.sizeValue).margin({ right: 30, left: 30 }) 
    } 
    .width('100%').height('100%').margin({ top: 30 }) 
  } 
}
分享
微博
QQ
微信
回复
2024-08-10 17:58:47
相关问题
如何设置自定义弹窗位置
1728浏览 • 1回复 待解决
HarmonyOS 自定义弹窗问题
225浏览 • 1回复 待解决
自定义弹窗自定义转场动画
654浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog问题
166浏览 • 1回复 待解决
如何自定义弹窗中再次弹窗
1877浏览 • 1回复 待解决
自定义弹窗如何嵌套使用
843浏览 • 1回复 待解决
如何自定义popup弹窗布局?
190浏览 • 2回复 待解决
HarmonyOS 自定义弹窗遮罩未全屏
129浏览 • 1回复 待解决
如何去除自定义弹窗白色背景
1847浏览 • 1回复 待解决
使用自定义弹窗实现分享弹窗
178浏览 • 1回复 待解决
HarmonyOS 自定义Dialog显示问题
91浏览 • 1回复 待解决
自定义弹窗使用相关问题
630浏览 • 1回复 待解决
如何理解自定义弹窗gridCount参数
1837浏览 • 1回复 待解决
自定义弹窗大小如何自适应内容
1984浏览 • 1回复 待解决
自定义弹窗变量如何传递给页面
2171浏览 • 1回复 待解决