HarmonyOS 自定义控件怎么重写width方法?

自定义一个控件,需要重写组件的width方法,把用户设置的width变成64的整数倍

自定义组件定义代码:

class Options {
  id: string = ''
  onLoad: Function = () => {
  };
}

@Builder
function overBuilder($$: Options) {
  XComponent({
    id: $$.id,
    libraryname: 'irtcrender',
    type: XComponentType.TEXTURE
  }).onLoad((iRtcRender: IRtcRender) => {
    $$.onLoad(iRtcRender);
  }).width('100%')
    .height('100%')
}

@Component
export struct IRtcComponent {
  @Prop Id: string;

  onLoad = (iRtcRender: IRtcRender): void => {
  }

  build() {
    overBuilder({ id: this.Id, onLoad: this.onLoad })
  }

  width(value: Length): CommonAttribute {
    //value = Math.ceil(value / 64) * 64
    super.width(value);
    return this;
  }
}

页面调用代码

IRtcComponent({
  Id: 'localRender',
  onLoad: (iRtcRender: IRtcRender) => {
    this.mLocalRender = iRtcRender;
    Logger.info(TAG, 'onLoad', `this.mLocalRender = ${JSON.stringify(this.mLocalRender)}`);
  }
})
  .alignRules({
    center: { anchor: '__container__', align: VerticalAlign.Center },
    middle: { anchor: '__container__', align: HorizontalAlign.Center }
  })
  .backgroundColor(Color.Black)
  .width('512px')
  .height('640px')

目前测试下来没有走到我自己定义的width方法

阅读 546
1 个回答

CommonAttribute 目前不支持自定义控件,可以手动要求应用方设置宽度为64的整数倍,或者先获取父组件宽度,再设置XComponent宽度

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