鸿蒙PC UI控件库 - 品牌标识系统详解

在这里插入图片描述

系列文章第1篇 | 作者:红目香薰 | 更新时间:2025年

📖 前言

随着鸿蒙PC平台的快速发展,越来越多的开发者开始为PC端开发应用。为了提升开发效率,我们开发了一套完整的、可复用的第三方UI控件库。本系列文章将详细介绍每个控件的使用方法、最佳实践和高级特性。

本文作为系列文章的第一篇,将重点介绍控件库的品牌标识系统,这是整个控件库的核心特性之一。


🎯 什么是品牌标识系统?

品牌标识系统是这套控件库的独特设计,每个控件都会在左下角显示一个独特的标识(圆圈内红色文字,类似 © 符号)。这个设计有以下几个目的:

  1. 品牌识别:让使用者一眼就能识别出这是来自我们控件库的组件
  2. 视觉统一:确保所有控件都有一致的视觉特征
  3. 开源标识:作为开源项目,标识有助于推广和识别

标识样式特点

  • 位置:控件左下角
  • 样式:圆圈内红色文字
  • 默认文字"PC"(可自定义)
  • 默认大小16vp(可调整)
  • 颜色方案
    • 文字颜色:#FF3B30(红色)
    • 边框颜色:#E5E5E5(浅灰色)
    • 背景颜色:#FFFFFF(白色)

🚀 快速开始

项目层级

cmd的tree无法显示所有列表,我这里给了个代码的结构。

entry/src/main/ets/
├── components/                    # 控件库主目录
│   ├── common/                    # 通用组件
│   │   ├── BaseComponent.ets      # 基础组件包装器(自动添加品牌标识)
│   │   ├── BrandBadge.ets         # 品牌标识组件(圆圈内红字)
│   │   └── index.ets              # 统一导出文件
│   └── README.md                   # 控件库使用指南
│
├── theme/                          # 主题配置系统
│   ├── BrandTheme.ets             # 品牌标识主题配置
│   ├── ComponentTheme.ets         # 控件库全局主题配置
│   └── README.md                   # 主题配置说明文档
│
├── pages/                          # 页面文件
│   ├── Index.ets                  # 主页面(展示控件示例)
│   └── BrandBadgeDemo.ets         # 品牌标识演示页面
│
├── entryability/                   # 应用入口能力
│   └── EntryAbility.ets           # 应用主入口
│
└── entrybackupability/             # 备份能力
    └── EntryBackupAbility.ets     # 备份入口

安装和引入

首先,将控件库代码复制到你的项目中,然后引入组件:

import { BaseComponent, BrandBadge } from '../components/common'

方式一:使用 BaseComponent 包装器(推荐)

BaseComponent 是最简单的使用方式,它会自动为你的控件添加品牌标识:

@Entry
@Component
struct MyPage {
  build() {
    Column() {
      // 使用 BaseComponent 包装你的控件
      BaseComponent({
        brandText: 'PC',           // 标识文字,默认 'PC'
        showBrand: true,            // 是否显示,默认 true
        brandSize: 16,              // 标识大小,默认 16vp
        componentWidth: '100%',     // 组件宽度,默认 '100%'
        componentHeight: 200        // 组件高度,默认 '100%'
      }) {
        // 你的控件内容
        Column({ space: 10 }) {
          Text('我的第一个控件')
            .fontSize(18)
            .fontWeight(FontWeight.Bold)
          
          Text('这是使用 BaseComponent 包装的控件')
            .fontSize(14)
            .fontColor('#666666')
        }
        .width('100%')
        .height('100%')
        .padding(20)
        .backgroundColor('#F5F5F5')
        .borderRadius(8)
      }
    }
    .width('100%')
    .height('100%')
    .padding(20)
  }
}

优点

  • ✅ 使用简单,一行代码即可
  • ✅ 自动处理标识位置
  • ✅ 支持统一配置

方式二:手动添加 BrandBadge

如果你需要更灵活的控制,可以手动添加 BrandBadge 组件:

@Entry
@Component
struct MyPage {
  build() {
    Stack({ alignContent: Alignment.BottomStart }) {
      // 你的控件内容
      Column({ space: 10 }) {
        Text('我的第二个控件')
          .fontSize(18)
          .fontWeight(FontWeight.Bold)
        
        Text('这是手动添加品牌标识的控件')
          .fontSize(14)
          .fontColor('#666666')
      }
      .width('100%')
      .height(200)
      .padding(20)
      .backgroundColor('#EFF6FF')
      .borderRadius(8)
      
      // 手动添加品牌标识
      BrandBadge({
        text: 'PC',
        badgeSize: 18,
        visible: true,
        textColor: '#FF3B30',
        circleBorderColor: '#E5E5E5',
        circleBackgroundColor: '#FFFFFF'
      })
    }
    .width('100%')
    .height(200)
  }
}

优点

  • ✅ 更灵活的位置控制
  • ✅ 可以自定义每个控件的标识样式
  • ✅ 适合特殊布局需求

⚙️ 品牌标识配置

核心设计理念

所有配置都在代码中,不依赖任何JSON文件!

这个设计让其他项目引入控件库后,可以直接修改配置,无需修改JSON资源文件,大大提升了灵活性。

配置方式一:直接修改静态属性(推荐)

import { BrandTheme } from '../theme/BrandTheme'

// 在应用启动时(如 EntryAbility.onCreate)修改配置
BrandTheme.BRAND_TEXT = 'HWPC'              // 修改标识文字
BrandTheme.BRAND_TEXT_COLOR = '#FF0000'     // 修改文字颜色
BrandTheme.BRAND_BORDER_COLOR = '#CCCCCC'   // 修改边框颜色
BrandTheme.BRAND_BACKGROUND_COLOR = '#F0F0F0' // 修改背景颜色
BrandTheme.BRAND_SIZE = 20                   // 修改标识大小

配置方式二:批量设置

import { BrandTheme } from '../theme/BrandTheme'

// 批量设置所有配置
BrandTheme.setConfig({
  text: 'HWPC',                    // 标识文字
  size: 18,                        // 标识大小
  textColor: '#FF3B30',            // 文字颜色
  borderColor: '#E5E5E5',          // 边框颜色
  backgroundColor: '#FFFFFF',      // 背景颜色
  textColorDark: '#FF6B6B',        // 暗色模式文字颜色
  borderColorDark: '#4A4A4A',      // 暗色模式边框颜色
  backgroundColorDark: '#1C1C1E'   // 暗色模式背景颜色
})

配置方式三:获取主题颜色(支持暗色模式)

import { BrandTheme } from '../theme/BrandTheme'

// 检测系统主题
const isDark = // 你的主题检测逻辑

// 获取对应主题的颜色
const textColor = BrandTheme.getTextColor(isDark)
const borderColor = BrandTheme.getBorderColor(isDark)
const backgroundColor = BrandTheme.getBackgroundColor(isDark)

🎨 完整配置示例

在应用启动时配置

// EntryAbility.ets
import { BrandTheme } from './theme/BrandTheme'
import { ComponentTheme } from './theme/ComponentTheme'

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
    // 配置品牌标识
    BrandTheme.setConfig({
      text: 'HWPC',
      size: 18,
      textColor: '#FF3B30',
      borderColor: '#E5E5E5',
      backgroundColor: '#FFFFFF'
    })
    
    // 配置全局主题
    ComponentTheme.setTheme({
      primaryColor: '#007AFF',
      textPrimary: '#000000',
      backgroundColor: '#FFFFFF',
      borderRadius: 8,
      spacing: 16
    })
    
    // ... 其他初始化代码
  }
}

📚 API 参考

BrandBadge 组件

属性说明
属性名类型默认值说明
textstring'PC'标识文字
badgeSizenumber16标识大小(vp)
visiblebooleantrue是否显示
textColorstring'#FF3B30'文字颜色
circleBorderColorstring'#E5E5E5'圆圈边框颜色
circleBackgroundColorstring'#FFFFFF'圆圈背景颜色
使用示例
BrandBadge({
  text: 'PC',
  badgeSize: 18,
  visible: true,
  textColor: '#FF3B30',
  circleBorderColor: '#E5E5E5',
  circleBackgroundColor: '#FFFFFF'
})

BaseComponent 组件

属性说明
属性名类型默认值说明
brandTextstring'PC'品牌标识文字
showBrandbooleantrue是否显示品牌标识
brandSizenumber16品牌标识大小(vp)
textColorstringBrandTheme 读取文字颜色
badgeBorderColorstringBrandTheme 读取边框颜色
badgeBackgroundColorstringBrandTheme 读取背景颜色
componentWidthstring | number'100%'组件宽度
componentHeightstring | number'100%'组件高度
使用示例
BaseComponent({
  brandText: 'PC',
  showBrand: true,
  brandSize: 18,
  componentWidth: '100%',
  componentHeight: 200
}) {
  // 你的控件内容
}

BrandTheme 类

静态属性
属性名类型默认值说明
BRAND_TEXTstring'PC'品牌标识文字
BRAND_SIZEnumber16品牌标识大小(vp)
BRAND_TEXT_COLORstring'#FF3B30'文字颜色
BRAND_BORDER_COLORstring'#E5E5E5'边框颜色
BRAND_BACKGROUND_COLORstring'#FFFFFF'背景颜色
BRAND_TEXT_COLOR_DARKstring'#FF6B6B'暗色模式文字颜色
BRAND_BORDER_COLOR_DARKstring'#4A4A4A'暗色模式边框颜色
BRAND_BACKGROUND_COLOR_DARKstring'#1C1C1E'暗色模式背景颜色
静态方法
// 获取文字颜色(根据主题)
static getTextColor(isDark: boolean = false): string

// 获取边框颜色(根据主题)
static getBorderColor(isDark: boolean = false): string

// 获取背景颜色(根据主题)
static getBackgroundColor(isDark: boolean = false): string

// 批量设置配置
static setConfig(config: BrandConfig): void

💡 最佳实践

1. 统一配置管理

建议在应用启动时统一配置品牌标识,而不是在每个组件中单独设置:

// ✅ 推荐:在 EntryAbility 中统一配置
BrandTheme.BRAND_TEXT = 'HWPC'
BrandTheme.BRAND_SIZE = 18

// ❌ 不推荐:在每个组件中单独设置
BaseComponent({ brandText: 'HWPC', brandSize: 18 }) { ... }

2. 使用 BaseComponent 包装器

对于大多数场景,推荐使用 BaseComponent 包装器,它更简单且统一:

// ✅ 推荐:使用 BaseComponent
BaseComponent() {
  // 你的控件
}

// ⚠️ 仅在需要特殊布局时使用手动方式
Stack() {
  // 你的控件
  BrandBadge()
}

3. 支持暗色模式

如果你的应用支持暗色模式,记得配置暗色模式的颜色:

BrandTheme.setConfig({
  textColor: '#FF3B30',           // 浅色模式
  textColorDark: '#FF6B6B',       // 暗色模式
  borderColor: '#E5E5E5',
  borderColorDark: '#4A4A4A',
  backgroundColor: '#FFFFFF',
  backgroundColorDark: '#1C1C1E'
})

4. 自定义品牌标识文字

你可以将标识文字改为你的品牌名称或项目名称:

// 使用项目名称
BrandTheme.BRAND_TEXT = 'MyApp'

// 使用品牌缩写
BrandTheme.BRAND_TEXT = 'HWPC'

// 使用特殊字符(单字符推荐)
BrandTheme.BRAND_TEXT = '★'

🔍 常见问题

Q1: 如何隐藏品牌标识?

// 方式1:在 BaseComponent 中设置
BaseComponent({ showBrand: false }) {
  // 你的控件
}

// 方式2:在 BrandBadge 中设置
BrandBadge({ visible: false })

Q2: 如何修改标识位置?

品牌标识默认在左下角。如果需要调整位置,可以使用手动方式:

Stack({ alignContent: Alignment.TopEnd }) {  // 改为右上角
  // 你的控件
  BrandBadge()
}

Q3: 如何在不同控件中使用不同大小的标识?

// 方式1:在 BaseComponent 中单独设置
BaseComponent({ brandSize: 20 }) { ... }
BaseComponent({ brandSize: 14 }) { ... }

// 方式2:在 BrandBadge 中单独设置
BrandBadge({ badgeSize: 20 })
BrandBadge({ badgeSize: 14 })

Q4: 配置修改后不生效?

确保在应用启动时(如 EntryAbility.onCreate)修改配置,而不是在组件内部:

// ✅ 正确:在 EntryAbility 中配置
export default class EntryAbility extends UIAbility {
  onCreate(...) {
    BrandTheme.BRAND_TEXT = 'HWPC'
  }
}

// ❌ 错误:在组件内部配置(不推荐)
@Component
struct MyComponent {
  build() {
    BrandTheme.BRAND_TEXT = 'HWPC'  // 不推荐
  }
}

📝 完整示例代码

示例1:基础使用

import { BaseComponent } from '../components/common'

@Entry
@Component
struct Example1 {
  build() {
    Column() {
      BaseComponent({
        componentWidth: '90%',
        componentHeight: 150
      }) {
        Column({ space: 10 }) {
          Text('基础示例')
            .fontSize(20)
            .fontWeight(FontWeight.Bold)
          
          Text('这是使用 BaseComponent 的基础示例')
            .fontSize(14)
            .fontColor('#666666')
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .padding(20)
        .backgroundColor('#F9FAFB')
        .borderRadius(12)
      }
    }
    .width('100%')
    .height('100%')
    .padding(20)
    .justifyContent(FlexAlign.Center)
  }
}

示例2:不同大小的标识

import { BaseComponent } from '../components/common'

@Entry
@Component
struct Example2 {
  build() {
    Row({ space: 20 }) {
      // 小标识
      BaseComponent({
        brandSize: 14,
        componentWidth: '30%',
        componentHeight: 100
      }) {
        Text('小标识')
          .fontSize(16)
      }
      
      // 中标识
      BaseComponent({
        brandSize: 18,
        componentWidth: '30%',
        componentHeight: 100
      }) {
        Text('中标识')
          .fontSize(16)
      }
      
      // 大标识
      BaseComponent({
        brandSize: 24,
        componentWidth: '30%',
        componentHeight: 100
      }) {
        Text('大标识')
          .fontSize(16)
      }
    }
    .width('100%')
    .padding(20)
  }
}

示例3:自定义颜色

import { BrandBadge } from '../components/common'

@Entry
@Component
struct Example3 {
  build() {
    Stack({ alignContent: Alignment.BottomStart }) {
      Column() {
        Text('自定义颜色示例')
          .fontSize(18)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
      .height(150)
      .padding(20)
      .backgroundColor('#EFF6FF')
      .borderRadius(12)
      
      // 自定义颜色的品牌标识
      BrandBadge({
        text: 'PC',
        badgeSize: 20,
        textColor: '#007AFF',           // 蓝色文字
        circleBorderColor: '#007AFF',   // 蓝色边框
        circleBackgroundColor: '#E3F2FD' // 浅蓝背景
      })
    }
    .width('90%')
    .height(150)
  }
}

🎯 总结

品牌标识系统是这套鸿蒙PC UI控件库的核心特性之一,它提供了:

  1. 简单易用:两种使用方式,满足不同需求
  2. 灵活配置:所有配置都在代码中,方便修改
  3. 统一视觉:确保所有控件都有一致的品牌标识
  4. 暗色模式支持:完整的暗色模式配置

在下一篇文章中,我们将介绍控件库的第一个实际控件:PrimaryButton(主要按钮),敬请期待!


下一篇预告:《鸿蒙PC UI控件库 - PrimaryButton 主要按钮详解》


本文是鸿蒙PC UI控件库系列文章的第1篇,后续将陆续发布更多控件的详细教程。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

红目香薰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值