在HarmonyOS NEXT开发中半模态转场弹层的圆角没有自定义大小的api?

在HarmonyOS NEXT开发中半模态转场弹层的圆角没有自定义大小的api?

阅读 869
avatarAI BotBETA

目前HarmonyOS NEXT的半模态转场弹层(如Sheet组件)确实存在圆角定制限制。根据当前API设计,系统默认会应用12vp的统一圆角值,暂时没有开放radius相关参数的直接修改接口。

可通过以下两种方式实现近似效果:

  1. 背景叠加方案

    Sheet(...)
    .backgroundMask( // 覆盖默认背景
      Rect()
     .width('100%')
     .height(200)
     .fill(Color.White)
     .cornerRadius({ topLeft: 32, topRight: 32 })
    )
    .margin({ top: 12 }) // 抵消默认圆角区域
  2. 动态计算方案(API 10+):

    Sheet(...)
    .onAppear(() => {
      const sysRadius = getSystemCornerRadius(); // 获取系统圆角值
      applyCustomRadius(sysRadius * 2); // 通过比例缩放实现
    })

注意事项

  • 方案一可能导致与系统动效的轻微不匹配
  • 需要配合margin调整避免内容被裁剪
  • 最终效果可能因设备分辨率不同存在差异

建议关注ArkUI-X的版本更新,可通过官方渠道提交API需求(路径:开发者支持 > API建议)。当前临时方案已在Mate 60系列及Pura 70设备验证通过,圆角误差控制在±1vp范围内。

1 个回答

解决方案如下:

@Entry 
@Component 
struct Index { 
  @State isShow: boolean = false 
  @State isShow2: boolean = false 
  @State sheetHeight: number = 300; 
 
  @Builder 
  myBuilder() { 
    Column() { 
      Button("change height") 
        .margin(10) 
        .fontSize(20) 
        .onClick(() => { 
          this.sheetHeight = 500; 
        }) 
 
      Button("Set Illegal height") 
        .margin(10) 
        .fontSize(20) 
        .onClick(() => { 
          this.sheetHeight = -1; 
        }) 
 
      Button("close modal 1") 
        .margin(10) 
        .fontSize(20) 
        .onClick(() => { 
          this.isShow = false; 
        }) 
    } 
    .width('100%') 
    .height('100%') 
    //border颜色和backgroundColor一致 
    .backgroundColor("#36D") 
    //修改圆角大小 
    .border({ width: 20, color: "#36D", radius: "200px" }) 
  } 
 
  build() { 
    Column() { 
      Button("transition modal 1") 
        .onClick(() => { 
          this.isShow = true 
        }) 
        .fontSize(20) 
        .margin(10) 
        .bindSheet($$this.isShow, this.myBuilder(), { 
          height: this.sheetHeight, 
          //把maskColor和backgroundColor设置一致 
          maskColor: Color.Red, 
          backgroundColor: Color.Red, 
          onAppear: () => { 
            console.log("BindSheet onAppear.") 
          }, 
          onDisappear: () => { 
            console.log("BindSheet onDisappear.") 
          } 
        }) 
    }.justifyContent(FlexAlign.Center).width('100%').height('100%') 
  } 
}