HarmonyOS web组件怎么获取h5的高度?

客户提供h5链接,用web去加载,但是web不给高度的话,默认是100%高度,客户希望能自使用他的高度,如果h5很长,web就展示很长,不要在web中滚动,如果只有一两行,那就只给一两行的高度。

阅读 560
1 个回答

参考demo:

import { webview } from '@kit.ArkWeb';

@Entry
@Component
struct Index {
  webContent: string =
    "<p>文本内容</p><p><img src=\"xxx\" title=\"xxxx\" alt=\"xx\"/></p>"
  controller: webview.WebviewController = new webview.WebviewController();

  build() {
    Column() {
      Web({ src: '', controller: this.controller, renderMode: RenderMode.SYNC_RENDER })
        .layoutMode(WebLayoutMode.FIT_CONTENT)
        .onControllerAttached(() => {
          this.controller.loadData(this.webContent, "text/html", "UTF-8", 'xxx')
        })
    }.backgroundColor(Color.Blue).height('100%').width('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进