HarmonyOS NEXT学习——@Styles、@Extend、stateStyles

@Styles装饰器 定义组件重用样式

  • 仅支持通用属性通用事件
  • 不支持参数
  • 可以定义全局和组件内使用,全局使用需要加function
// 全局
@Styles function functionName() { ... }

// 在组件内
@Component
struct FancyUse {
  @Styles fancy() {
    .height(100)
  }
}
  • 组件内@Styles的优先级高于全局@Styles。
// 定义在全局的@Styles封装的样式
@Styles function globalFancy  () {
  .width(100)
  .height(100)
  .backgroundColor(Color.Pink)
}

@Entry
@Component
@Preview
struct FancyUse {
  @State heightValue: number = 100
  // 定义在组件内的@Styles封装的样式
  @Styles fancy() {
    .width(200)
    .height(this.heightValue)
    .backgroundColor(Color.Yellow)
    .onClick(() => {
      this.heightValue = 200
    })
  }

  build() {
    Column({ space: 10 }) {
      // 使用全局的@Styles封装的样式
      Text('FancyA')
        .globalFancy()
        .fontSize(30)
      // 使用组件内的@Styles封装的样式
      Text('FancyB')
        .fancy()
        .fontSize(30)
      //组件内优先级高于全局
      Text('FancyAB')
        .globalFancy()
        .fancy()
        .fontSize(30)
    }
    .alignItems(HorizontalAlign.Start)
  }
}

显示效果

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

  • @Styles用于样式的扩展
  • 可以传参数
  • 仅能全局定义
@Extend(Text) function fancyText(weightValue: number, color: Color,clickE:()=>void) {
  .fontStyle(FontStyle.Italic)
  .fontSize(weightValue)
  .backgroundColor(color)
  .onClick(clickE)
}
@Entry
@Component
struct FancyUse {
  @State label: string = 'Hello World'

  onClickHandler(){
    console.log('40')
  }
  build() {
    Column() {
      Text(`${this.label}`)
        .fancyText(30, Color.Blue,()=>{console.log('30')})
      Text(`${this.label}`)
        .fancyText(40, Color.Pink,()=>{this.onClickHandler()})
      Text(`${this.label}`)
        .fancyText(50, Color.Orange,()=>{console.log('50')})
    }
    .alignItems(HorizontalAlign.Start)
  }
}

在这里插入图片描述
三个点击事件
点击事件

stateStyles 多态样式

stateStyles是属性方法,可以根据UI内部状态来设置样式,类似于css伪类,但语法不同。ArkUI提供以下五种状态:

  • focused:获焦态。
  • normal:正常态。
  • pressed:按压态。
  • disabled:不可用态。
  • selected:选中态。

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

  build() {
    Column() {
      Text(`${this.label}`)
        .stateStyles({
          normal:{
            .backgroundColor(Color.Yellow)
          },
          pressed:{
            .backgroundColor(Color.Pink)
          }
        })
        .fontSize(40)
    }
    .alignItems(HorizontalAlign.Start)
  }
}

相关推荐

  1. 学习 学习

    2024-07-20 16:52:04       59 阅读
  2. 学期学习计划

    2024-07-20 16:52:04       39 阅读
  3. 学习笔记:机器学习

    2024-07-20 16:52:04       76 阅读
  4. C++学习-List学习

    2024-07-20 16:52:04       44 阅读
  5. opencv学习 机器学习

    2024-07-20 16:52:04       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 16:52:04       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 16:52:04       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 16:52:04       45 阅读
  4. Python语言-面向对象

    2024-07-20 16:52:04       55 阅读

热门阅读

  1. C++案例四:简易记事本程序

    2024-07-20 16:52:04       17 阅读
  2. DNS解析过程

    2024-07-20 16:52:04       17 阅读
  3. axios

    axios

    2024-07-20 16:52:04      23 阅读
  4. 使用Spring Boot和RabbitMQ实现消息驱动微服务

    2024-07-20 16:52:04       19 阅读
  5. postgresql 大于当前时间升序,小于当前时间降序

    2024-07-20 16:52:04       18 阅读
  6. 在 C# .NET 中丢弃变量

    2024-07-20 16:52:04       19 阅读
  7. 基于深度学习的故障检测

    2024-07-20 16:52:04       19 阅读
  8. 【Spring Boot 自定义配置项详解】

    2024-07-20 16:52:04       19 阅读
  9. 13、.Net相关的书籍 - .Net循序渐进系列文章

    2024-07-20 16:52:04       20 阅读