文档开发者中心
我的
|

ArkUI (方舟UI框架)

UI

HarmonyOS 5

HarmonyOS 图文混排的直接渲染和监听状态变量渲染效果不一致 (API12+)

同一个写法的组件,直接渲染和监听状态变量渲染的效果不一致。

收起
自动换行
深色代码主题
复制
@Entry
@Component
struct SpanTestPage {
  @State message: string = 'Hello World';
  @State isShow: boolean = false;

  build() {
    Column() {

      Button('click me').onClick((event: ClickEvent) => {
        this.isShow = true;
      }).margin(100)

      Text() {
        Span('****')
        CustomComp()
        Span('你好')
      }
      .fontSize(20)
      .border({ width: 2, color: '#0ff' })
      .margin(10)

      if (this.isShow) {
        Text() {
          Span('****')
          CustomComp()
          Span('你好')
        }
        .fontSize(20)
        .border({ width: 2, color: '#0ff' })
        .margin(10)
      }
    }
    .height('100%')
    .width('100%')
  }
}

@Component
struct CustomComp {
  build() {
    ImageSpan($r('app.media.startIcon'))
      .size({ width: '100px', height: '100px' })
      .verticalAlign(ImageSpanAlignment.BASELINE)
  }
}
点赞
收藏
回复
1
分享
举报
浏览22 发布于2026-04-02 11:25未知归属地
全部评论
最多点赞
最新发布
最早发布

目前Text组件内部不支持使用自定义组件,可能会出现问题,可以继续使用@Builder的方式,通过AttributeUpdater对ImageSpan修改属性,在参数中设置为undefined,在alt中设置图片,这样就可以动态修改了。

参考代码:

收起
自动换行
深色代码主题
复制
import { AttributeUpdater, ImageSpanModifier } from '@kit.ArkUI';
import { image } from '@kit.ImageKit';

class MyTextModifier extends AttributeUpdater<ImageSpanAttribute, ImageSpanInterface> {
  initializeModifier(instance: ImageSpanAttribute) {
  }
}

let modifier = new MyTextModifier();

function getPixelMap(context: Context, resource: Resource) {
  let data = context.resourceManager.getMediaContentSync(resource)
  let imageSource = image.createImageSource(data.buffer);
  return imageSource.createPixelMapSync();
}

@Entry
@Component
struct SpanTestPage {
  @State message: ResourceStr = $r('app.media.app_icon');
  @State isShow: boolean = false;

  aboutToAppear(): void {
    modifier = new MyTextModifier();
  }

  build() {
    NavDestination() {
      Column() {
        Button('click me').onClick((event: ClickEvent) => {
          this.isShow = !this.isShow;
          // modifier.updateConstructorParams($r('app.media.app_icon'))
          // modifier.attribute?.textBackgroundStyle({color: '#000'})

          // let id = setTimeout(() => {
          // modifier.updateConstructorParams($r('app.media.app_icon'))
          modifier.attribute?.alt(getPixelMap(getContext(this), $r('app.media.app_icon')));
          // clearTimeout(id)
          // }, 1000)
        }).margin(100)

        Image(this.message)
          .width('100px')
          .height('100px')

        Text() {
          Span('****')
          builder.builder()
          // ImageSpan($r('app.media.startIcon'))
          //   .size({ width: '100px', height: '100px' })
          //   .verticalAlign(ImageSpanAlignment.BASELINE)
          Span('您好')
        }
        .fontSize(20)
        .border({ width: 2, color: '#0ff' })
        .margin(10)
        .backgroundColor('#f00')
      }
      .height('100%')
      .width('100%')
    }

  }
}

@Builder
function inLineImage() {
  ImageSpan(undefined)
    .alt(getPixelMap(getContext(), $r('app.media.foreground')))
    .size({ width: '100px', height: '100px' })
    .verticalAlign(ImageSpanAlignment.BASELINE)
    .attributeModifier(modifier)
}

const builder = wrapBuilder(inLineImage);
展开全部
1楼回复于2026-04-02 11:25 未知归属地
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。