在HarmonyOS NEXT开发中自定义组件如何获取高度?

阅读 688
1 个回答

自定义组件获取组件高度可使用.onAreaChange()回调函数,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-refere...

@Component 
struct HelloComponent { 
  @State message: string = 'Hello, World!'; 
 
  build() { 
    // HelloComponent自定义组件组合系统组件Row和Text 
    Row() { 
      Text(this.message) 
        .onClick(() => { 
          // 状态变量message的改变驱动UI刷新,UI从'Hello, World!'刷新为'Hello, ArkUI!' 
          this.message = 'Hello, ArkUI!'; 
        }) 
    } 
  } 
} 
 
class HelloComponentParam { 
  message: string = "" 
} 
 
@Entry 
@Component 
struct ParentComponent { 
  param: HelloComponentParam = { 
    message: 'Hello, World!' 
  } 
  @State sizeValue: string = '' 
 
  build() { 
    Column() { 
      Text('ArkUI message') 
      HelloComponent(this.param) 
        .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) 
        }) 
      Divider() 
      Text('new area is: \n' + this.sizeValue).margin({ right: 30, left: 30 }) 
    } 
  } 
}

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进