#HarmonyOS:@Extend装饰器:定义扩展组件样式

使用示例

常规写法

@Entry
@Component
struct FancyUse {
  @State label: string = 'Hello World'

  build() {
    Row({ space: 10 }) {
      Text(`${this.label}`)
        .fontStyle(FontStyle.Italic)
        .fontWeight(100)
        .backgroundColor(Color.Blue)
      Text(`${this.label}`)
        .fontStyle(FontStyle.Italic)
        .fontWeight(200)
        .backgroundColor(Color.Pink)
      Text(`${this.label}`)
        .fontStyle(FontStyle.Italic)
        .fontWeight(300)
        .backgroundColor(Color.Orange)
    }.margin('20%')
  }
}

公用方法

@Extend(Text) function fancyText(weightValue: number, color: Color) {
  .fontStyle(FontStyle.Italic)
  .fontWeight(weightValue)
  .backgroundColor(color)
}

使用公用方法

@Entry
@Component
struct FancyUse {
  @State label: string = 'Hello World'

  build() {
    Row({ space: 10 }) {
      Text(`${this.label}`)
        .fancyText(100, Color.Blue)
      Text(`${this.label}`)
        .fancyText(200, Color.Pink)
      Text(`${this.label}`)
        .fancyText(300, Color.Orange)
    }.margin('20%')
  }
}

通俗的示例 @Extend(Text) function opacityTextStyle(fontSize: number, fontColor:string)

@Component
export default struct TargetInformation {
  build() {
    Row() {
      Image($r("app.media.icon"))
        .opacityImageStyle() // 扩展Image组件样式
      Column() {
        Text('第一季度运营目标')
          .opacityTextStyle(24, '#ffdd3355')
        Text('实现')
          .opacityTextStyle(16, '#999') // 扩展Text组件样式
      }
      .alignItems(HorizontalAlign.Start)
      .margin({ left: '20fp'})
    }
  }
}

/**
 * 自定义Image组件的样式  @Extend装饰器:定义扩展组件样式
 */
@Extend(Image) function opacityImageStyle() {
  .width("80vp")
  .height("80vp")
  .objectFit(ImageFit.Fill)
}

/**
 * 自定义Text组件的样式  @Extend装饰器:定义扩展组件样式
 */
@Extend(Text) function opacityTextStyle(fontSize: number, fontColor:string) {
  .fontSize(fontSize)
  .fontColor(fontColor)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值