HarmonyOS 如何动态的向“页面”里插入一个View

请问怎么能使用代码的方式动态的向一个页面里添加和移除一个View。

场景是我们有一个全局的运营模块,这个全局的模块获取到需要展示的运营数据后,希望能对页面开发来说无感的插入一个View并控制这个View的显示和隐藏。

HarmonyOS
2025-01-09 15:11:54
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

示例参考如下:

import { ComponentContent } from '@ohos.arkui.node'

@Builder
function buildText() {
  Column() {
    Text('hello')
      .width('100%')
      .height(50)
      .backgroundColor(Color.Yellow)
  }
}

@Entry
@Component
export struct FrameNodePage {

  build() {
    Row() {
      Column() {
        Column(){

        }
        .id("mainContainer")
        .height(300)
        .width('100%')
        .borderWidth(1)

        Text("添加元素")
          .align(Alignment.Center)
          .fontSize(20)
          .fontFamily('HarmonyOS Sans')
          .height('100%')
          .onClick(()=>{
            let uiContext = this.getUIContext();
            let root = uiContext.getFrameNodeById("mainContainer");
            let component = new ComponentContent<Object>(uiContext, wrapBuilder(buildText))
            root?.addComponentContent(component);
          });
      }
      .height('100%')
    }
  }

  aboutToAppear(): void {
  }
}
  • 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.

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-arkui-uicontext-V5#uicontext

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-arkui-framenode-V5#%E8%8A%82%E7%82%B9%E6%93%8D%E4%BD%9C%E7%A4%BA%E4%BE%8B

分享
微博
QQ
微信
回复
2025-01-09 18:23:35
相关问题
HarmonyOS 如何添加一个悬浮View
925浏览 • 1回复 待解决
SQL插入一个数据时,如何获取ID?
3587浏览 • 2回复 待解决
一个mysql表插入截断问题有懂吗?
3055浏览 • 1回复 待解决
鸿蒙如何实现一个本地定时通知?
623浏览 • 0回复 待解决
如何新开一个半透明页面
788浏览 • 1回复 待解决
HarmonyOS view动态添加
840浏览 • 1回复 待解决