ArkUI 资源规范:颜色和文案要成组维护

举报
蓝瘦的蜕变 发表于 2026/07/25 10:08:00 2026/07/25
【摘要】 资源规范的重点不是把所有内容都塞进 JSON,而是让同一类资源成组维护。深浅色要保证 base/dark 同名颜色 key,国际化要保证 base/enUS/frFR 同名文案 key。 本篇边界 这篇只讲 color.json、string.json、资源限定词和 key 命名。Demo 不做运行时扫描资源文件,不做

ArkUI 资源规范:颜色和文案要成组维护

资源规范的重点不是把所有内容都塞进 JSON,而是让同一类资源成组维护。深浅色要保证 base/dark 同名颜色 key,国际化要保证 base/en_US/fr_FR 同名文案 key。

本篇边界

这篇只讲 color.jsonstring.json、资源限定词和 key 命名。Demo 不做运行时扫描资源文件,不做全局语言切换,不接在线翻译,也不扩展成复杂设计系统。

交互预期

  • 页面颜色全部通过 app.color.resource_spec_* 资源读取。
  • 订单文案全部通过 app.string.resource_spec_* 资源读取。
  • 点击“查看本页资源规则”后,下方状态文本会展示本页验收规则。

资源配置

颜色资源在 resources/base/element/color.jsonresources/dark/element/color.json 中保持同名 key,例如:

{
  "name": "resource_spec_brand",
  "value": "#2563EB"
}

文案资源在 resources/base/element/string.jsonresources/en_US/element/string.jsonresources/fr_FR/element/string.json 中保持同名 key,例如:

{
  "name": "resource_spec_order_title",
  "value": "订单中心"
}

完整示例代码

@Entry
@Component
struct ArkUIResourceSpecDemo {
  @State hasChecked: boolean = false

  @Builder
  colorSectionTitle() {
    Column({ space: 6 }) {
      Text($r('app.string.resource_spec_color_title'))
        .fontSize(19)
        .fontWeight(FontWeight.Bold)
        .fontColor($r('app.color.resource_spec_text_primary'))
      Text($r('app.string.resource_spec_color_desc'))
        .fontSize(13)
        .fontColor($r('app.color.resource_spec_text_secondary'))
        .lineHeight(20)
    }
    .width('100%')
    .alignItems(HorizontalAlign.Start)
  }

  @Builder
  stringSectionTitle() {
    Column({ space: 6 }) {
      Text($r('app.string.resource_spec_string_title'))
        .fontSize(19)
        .fontWeight(FontWeight.Bold)
        .fontColor($r('app.color.resource_spec_text_primary'))
      Text($r('app.string.resource_spec_string_desc'))
        .fontSize(13)
        .fontColor($r('app.color.resource_spec_text_secondary'))
        .lineHeight(20)
    }
    .width('100%')
    .alignItems(HorizontalAlign.Start)
  }

  @Builder
  namingSectionTitle() {
    Column({ space: 6 }) {
      Text($r('app.string.resource_spec_naming_title'))
        .fontSize(19)
        .fontWeight(FontWeight.Bold)
        .fontColor($r('app.color.resource_spec_text_primary'))
      Text($r('app.string.resource_spec_naming_desc'))
        .fontSize(13)
        .fontColor($r('app.color.resource_spec_text_secondary'))
        .lineHeight(20)
    }
    .width('100%')
    .alignItems(HorizontalAlign.Start)
  }

  @Builder
  validationSectionTitle() {
    Column({ space: 6 }) {
      Text($r('app.string.resource_spec_validation_title'))
        .fontSize(19)
        .fontWeight(FontWeight.Bold)
        .fontColor($r('app.color.resource_spec_text_primary'))
      Text($r('app.string.resource_spec_validation_desc'))
        .fontSize(13)
        .fontColor($r('app.color.resource_spec_text_secondary'))
        .lineHeight(20)
    }
    .width('100%')
    .alignItems(HorizontalAlign.Start)
  }

  @Builder
  brandColorRow() {
    Row({ space: 12 }) {
      Blank()
        .width(42)
        .height(42)
        .backgroundColor($r('app.color.resource_spec_brand'))
        .borderRadius(8)
      Column({ space: 4 }) {
        Text($r('app.string.resource_spec_color_brand_label'))
          .fontSize(15)
          .fontWeight(FontWeight.Bold)
          .fontColor($r('app.color.resource_spec_text_primary'))
        Text('resource_spec_brand')
          .fontSize(12)
          .fontColor($r('app.color.resource_spec_text_secondary'))
      }
      .layoutWeight(1)
      .alignItems(HorizontalAlign.Start)
    }
    .width('100%')
  }

  @Builder
  surfaceColorRow() {
    Row({ space: 12 }) {
      Blank()
        .width(42)
        .height(42)
        .backgroundColor($r('app.color.resource_spec_surface'))
        .border({ width: 1, color: $r('app.color.resource_spec_border') })
        .borderRadius(8)
      Column({ space: 4 }) {
        Text($r('app.string.resource_spec_color_surface_label'))
          .fontSize(15)
          .fontWeight(FontWeight.Bold)
          .fontColor($r('app.color.resource_spec_text_primary'))
        Text('resource_spec_surface')
          .fontSize(12)
          .fontColor($r('app.color.resource_spec_text_secondary'))
      }
      .layoutWeight(1)
      .alignItems(HorizontalAlign.Start)
    }
    .width('100%')
  }

  @Builder
  textColorRow() {
    Row({ space: 12 }) {
      Blank()
        .width(42)
        .height(42)
        .backgroundColor($r('app.color.resource_spec_text_primary'))
        .borderRadius(8)
      Column({ space: 4 }) {
        Text($r('app.string.resource_spec_color_text_label'))
          .fontSize(15)
          .fontWeight(FontWeight.Bold)
          .fontColor($r('app.color.resource_spec_text_primary'))
        Text('resource_spec_text_primary')
          .fontSize(12)
          .fontColor($r('app.color.resource_spec_text_secondary'))
      }
      .layoutWeight(1)
      .alignItems(HorizontalAlign.Start)
    }
    .width('100%')
  }

  @Builder
  colorResourcePanel() {
    Column({ space: 14 }) {
      this.brandColorRow()
      this.surfaceColorRow()
      this.textColorRow()
      Text($r('app.string.resource_spec_color_key_note'))
        .fontSize(12)
        .fontColor($r('app.color.resource_spec_text_secondary'))
        .lineHeight(18)
    }
    .width('100%')
    .padding(16)
    .borderRadius(14)
    .backgroundColor($r('app.color.resource_spec_panel_background'))
    .border({ width: 1, color: $r('app.color.resource_spec_border') })
    .alignItems(HorizontalAlign.Start)
  }

  @Builder
  stringResourcePanel() {
    Column({ space: 12 }) {
      Text($r('app.string.resource_spec_order_title'))
        .fontSize(22)
        .fontWeight(FontWeight.Bold)
        .fontColor($r('app.color.resource_spec_text_primary'))
      Text($r('app.string.resource_spec_order_empty'))
        .fontSize(14)
        .fontColor($r('app.color.resource_spec_text_secondary'))
        .lineHeight(22)
      Text($r('app.string.resource_spec_order_action'))
        .fontSize(14)
        .fontColor($r('app.color.resource_spec_button_text'))
        .padding({ left: 12, right: 12, top: 8, bottom: 8 })
        .backgroundColor($r('app.color.resource_spec_brand'))
        .borderRadius(8)
      Text($r('app.string.resource_spec_string_key_note'))
        .fontSize(12)
        .fontColor($r('app.color.resource_spec_text_secondary'))
        .lineHeight(18)
    }
    .width('100%')
    .padding(16)
    .borderRadius(14)
    .backgroundColor($r('app.color.resource_spec_panel_background'))
    .border({ width: 1, color: $r('app.color.resource_spec_border') })
    .alignItems(HorizontalAlign.Start)
  }

  @Builder
  namingPanel() {
    Column({ space: 10 }) {
      Text('resource_spec_page_background')
        .fontSize(13)
        .fontColor($r('app.color.resource_spec_text_primary'))
      Text('resource_spec_order_title')
        .fontSize(13)
        .fontColor($r('app.color.resource_spec_text_primary'))
      Text('resource_spec_order_action')
        .fontSize(13)
        .fontColor($r('app.color.resource_spec_text_primary'))
    }
    .width('100%')
    .padding(14)
    .backgroundColor($r('app.color.resource_spec_chip_background'))
    .borderRadius(10)
    .alignItems(HorizontalAlign.Start)
  }

  build() {
    Scroll() {
      Column({ space: 18 }) {
        Text($r('app.string.resource_spec_page_title'))
          .fontSize(26)
          .fontWeight(FontWeight.Bold)
          .fontColor($r('app.color.resource_spec_text_primary'))
          .width('100%')
        Text($r('app.string.resource_spec_page_desc'))
          .fontSize(14)
          .fontColor($r('app.color.resource_spec_text_secondary'))
          .lineHeight(22)
          .width('100%')

        this.colorSectionTitle()
        this.colorResourcePanel()

        this.stringSectionTitle()
        this.stringResourcePanel()

        this.namingSectionTitle()
        this.namingPanel()

        Button($r('app.string.resource_spec_run_check'))
          .height(46)
          .width('100%')
          .fontSize(15)
          .fontColor($r('app.color.resource_spec_button_text'))
          .backgroundColor($r('app.color.resource_spec_brand'))
          .onClick(() => {
            this.hasChecked = true
          })

        Text(this.hasChecked ? $r('app.string.resource_spec_feedback_checked') : $r('app.string.resource_spec_feedback_initial'))
          .width('100%')
          .fontSize(14)
          .fontColor($r('app.color.resource_spec_text_primary'))
          .lineHeight(22)
          .padding(12)
          .backgroundColor($r('app.color.resource_spec_chip_background'))
          .borderRadius(10)

        this.validationSectionTitle()
      }
      .width('100%')
      .padding(20)
      .alignItems(HorizontalAlign.Start)
    }
    .width('100%')
    .height('100%')
    .backgroundColor($r('app.color.resource_spec_page_background'))
  }
}
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。