HarmonyOS XComponent怎么采用组件安全区方案?

HarmonyOS XComponent怎么采用组件安全区方案?

HarmonyOS
2024-10-23 12:25:06
1204浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

写demo模拟了一下,不是XC的问题,应该是safearea不支持挂载在自定义组件下面,可以将safesrea属性直接挂载下XComponent组件下面可以达到效果。

@Entry  
@Component  
struct XCAndSafeArea {  
  @State message: string = 'Hello World';  
  
  build() {  
    Row() {  
      Column() {  
        // Text(this.message)  
        // .fontSize(50)  
        // .fontWeight(FontWeight.Bold)  
        Stack() {  
          zhongJianShang()  
          // .expandSafeArea([SafeAreaType.SYSTEM],[SafeAreaEdge.TOP,SafeAreaEdge.BOTTOM])  
  
          Row()  
            .width(50)  
            .height(50)  
            .backgroundColor(Color.Red)  
        }  
        // .expandSafeArea([SafeAreaType.SYSTEM],[SafeAreaEdge.TOP,SafeAreaEdge.BOTTOM])  
      }  
      .width('100%')  
    }  
    .height('100%')  
  }  
}  
  
@Component  
struct zhongJianShang {  
  build() {  
    Stack() {  
      xcDemo()  
    }  
    // .expandSafeArea([SafeAreaType.SYSTEM],[SafeAreaEdge.TOP,SafeAreaEdge.BOTTOM])  
  }  
}  
  
@Component  
struct xcDemo {  
  private surfaceId: string = ''  
  private xComponentContext: Record<string, () => void> = {}  
  xComponentController: XComponentController = new XComponentController()  
  
  build() {  
    Row() {  
      XComponent({  
        id: 'xcomponent',  
        type: XComponentType.SURFACE,  
        controller: this.xComponentController  
      })  
        .onLoad(() => {  
          this.surfaceId = this.xComponentController.getXComponentSurfaceId()  
        })  
          // Row()  
        .backgroundColor(Color.Blue)  
        .width('100%')  
        .height('100%')  
        .expandSafeArea([SafeAreaType.SYSTEM],[SafeAreaEdge.TOP,SafeAreaEdge.BOTTOM])  
    }  
  }  
}
  • 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.

请使用新增接口setXComponentSurfaceRect进行验证:

https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/arkui-ts/ts-basic-components-xcomponent.md#setxcomponentsurfacerect12

代码改造示例:

.onLoad(async () => {  
  this.surfaceId = this.mXComponentController.getXComponentSurfaceId();  
  display.getAllDisplays((err, data) => {  
    let screenWidth : number = data[0].width  
    let screenHeight : number = data[0].height  
    let surfaceRect: SurfaceRect = { offsetX: 0, offsetY: 0, surfaceWidth: screenWidth, surfaceHeight: screenHeight }  
    this.mXComponentController.setXComponentSurfaceRect(surfaceRect)  
  })  
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
分享
微博
QQ
微信
回复
2024-10-23 15:59:10
相关问题
HarmonyOS 安全区域问题
1038浏览 • 1回复 待解决
HarmonyOS 安全区域出错
837浏览 • 1回复 待解决
HarmonyOS 安全区域失效
814浏览 • 1回复 待解决
HarmonyOS 视频组件无法扩展其安全区
1141浏览 • 1回复 待解决
HarmonyOS 设置安全区域不生效
894浏览 • 1回复 待解决
HarmonyOS WebView安全区域问题
648浏览 • 1回复 待解决
HarmonyOS scroll安全区域问题
906浏览 • 1回复 待解决
HarmonyOS 页面底部流出安全区
908浏览 • 1回复 待解决
HarmonyOS 如何获取手机安全区域高度
850浏览 • 1回复 待解决
关于屏幕安全区域的问题咨询
1130浏览 • 1回复 待解决
Flutter 项目在设备安全区如何适配?
883浏览 • 1回复 待解决