HarmonyOS ArkUI Badge 徽标与 Chip 标签:消息红点、数字徽标与可选中标签
系列鸿蒙 HarmonyOS 6.1 新特性实战 · 第 39 篇Badge徽标用于在图标或按钮上附加消息计数、红点提醒或文字标记Chip标签则用于展示可操作的关键词标签。本篇演示 Badge 的数字、文字、位置三个维度以及用自定义 Row 实现可关闭、可选中的 Chip 标签组。运行效果初始状态Badge 数字徽标和 Chip 标签列表点击 1 消息后徽标数字递增一、Badge 数字徽标State msgCount: number 5 Badge({ count: this.msgCount, // 数字徽标 maxCount: 99, // 超过显示 99 position: BadgePosition.RightTop, style: { badgeSize: 18, badgeColor: #e74c3c, fontSize: 11 } }) { Text(✉).fontSize(36) // 被包裹的内容 }点击按钮更新数字Button(1 消息) .onClick(() { this.msgCount Math.min(this.msgCount 1, 999) }) Button(清空) .onClick(() { this.msgCount 0 })当count为 0 时Badge 自动隐藏不需要条件渲染。二、文字徽标value 属性// 文字标签NEW、HOT 等 Badge({ value: NEW, position: BadgePosition.RightTop, style: { badgeSize: 16, badgeColor: #ff6600, fontSize: 10 } }) { Text(新功能).padding(8).backgroundColor(#f5f5f5).borderRadius(4) } // 红点只显示一个圆点 Badge({ value: this.redDot ? ● : , position: BadgePosition.RightTop, style: { badgeSize: 12, badgeColor: #e74c3c, fontSize: 8 } }) { Text().fontSize(36) }三、BadgePosition 位置// RightTop右上角最常用消息通知 Badge({ count: 1, position: BadgePosition.RightTop, style: {...} }) { ... } // Right右侧居中 Badge({ count: 1, position: BadgePosition.Right, style: {...} }) { ... } // Left左侧居中 Badge({ count: 1, position: BadgePosition.Left, style: {...} }) { ... }四、自定义 Chip 标签ArkUI 6.1 中Chip组件在某些 SDK 版本不可用可用Row Text自行实现State chips: string[] [ArkTS, HarmonyOS, ArkUI, NEXT, 鸿蒙] State chipActive: boolean[] [true, false, true, false, false] // 可关闭标签 Row({ space: 4 }) { Text(chip).fontSize(13).fontColor(#0066ff) Text(×).fontSize(14).fontColor(#0066ff).fontWeight(FontWeight.Bold) .onClick(() { const arr this.chips.filter((_: string, i: number) i ! idx) const aArr this.chipActive.filter((_: boolean, i: number) i ! idx) this.chips arr this.chipActive aArr }) } .padding({ left: 10, right: 8, top: 5, bottom: 5 }) .backgroundColor(#e8f0ff).borderRadius(16).border({ width: 1, color: #b3d1ff })ArkTS 的 ForEach 注意事项不能在 ForEach 的 UI builder 回调内声明const变量因为Row没有flexWrap多个 Chip 需要按行拆分到不同 Row 容器// 分两行渲染避免 Row 没有 flexWrap 的限制 Column({ space: 8 }) { Row({ space: 8 }) { ForEach(this.chips.slice(0, 3), (chip: string, idx: number) { Row({ space: 4 }) { Text(chip).fontSize(13).fontColor(#0066ff) Text(×).fontSize(14).fontColor(#0066ff) .onClick(() { const arr this.chips.filter((_: string, i: number) i ! idx) this.chips arr }) } .padding({ left: 10, right: 8, top: 5, bottom: 5 }) .backgroundColor(#e8f0ff).borderRadius(16) }) } Row({ space: 8 }) { ForEach(this.chips.slice(3), (chip: string, relIdx: number) { Row({ space: 4 }) { Text(chip).fontSize(13).fontColor(#0066ff) Text(×).fontSize(14).fontColor(#0066ff) .onClick(() { // 用 relIdx 3 代替 const idx relIdx 3ArkTS 限制 const arr this.chips.filter((_: string, i: number) i ! relIdx 3) this.chips arr }) } .padding({ left: 10, right: 8, top: 5, bottom: 5 }) .backgroundColor(#e8f0ff).borderRadius(16) }) } }可选中标签ForEach(this.chips, (chip: string, idx: number) { Text(chip) .fontSize(13) .fontColor(this.chipActive[idx] ? #fff : #0066ff) .padding({ left: 12, right: 12, top: 6, bottom: 6 }) .backgroundColor(this.chipActive[idx] ? #0066ff : #e8f0ff) .borderRadius(16).border({ width: 1, color: #0066ff }) .onClick(() { const arr this.chipActive.slice() arr[idx] !arr[idx] this.chipActive arr }) })完整代码Entry Component struct Index { State msgCount: number 5 State redDot: boolean true State chips: string[] [ArkTS, HarmonyOS, ArkUI, NEXT, 鸿蒙] State chipActive: boolean[] [true, false, true, false, false] build() { Scroll() { Column({ space: 20 }) { Text(Badge 徽标与 Chip 标签组件) .fontSize(22).fontWeight(FontWeight.Bold).fontColor(#1a1a1a) // Badge 数字徽标 Column({ space: 16 }) { Text(一、Badge 数字徽标).fontSize(15).fontWeight(FontWeight.Bold).fontColor(#333) Row({ space: 32 }) { Column({ space: 6 }) { Badge({ count: this.msgCount, maxCount: 99, position: BadgePosition.RightTop, style: { badgeSize: 18, badgeColor: #e74c3c, fontSize: 11 } }) { Text(✉).fontSize(36) } Text(消息).fontSize(12).fontColor(#666) } Column({ space: 6 }) { Badge({ count: 3, maxCount: 99, position: BadgePosition.RightTop, style: { badgeSize: 16, badgeColor: #e74c3c } }) { Text().fontSize(36) } Text(购物车).fontSize(12).fontColor(#666) } Column({ space: 6 }) { Badge({ value: this.redDot ? ● : , position: BadgePosition.RightTop, style: { badgeSize: 12, badgeColor: #e74c3c, fontSize: 8 } }) { Text().fontSize(36) } Text(通知).fontSize(12).fontColor(#666) } } .width(100%).justifyContent(FlexAlign.SpaceEvenly).alignItems(VerticalAlign.Bottom) Row({ space: 8 }) { Button(1 消息).fontSize(12).height(36).backgroundColor(#e8f0ff).fontColor(#0066ff) .onClick(() { this.msgCount Math.min(this.msgCount 1, 999) }) Button(清空).fontSize(12).height(36).backgroundColor(#fff0f0).fontColor(#e74c3c) .onClick(() { this.msgCount 0 }) Button(this.redDot ? 关红点 : 开红点).fontSize(12).height(36) .backgroundColor(#e8f0ff).fontColor(#0066ff) .onClick(() { this.redDot !this.redDot }) } } .padding(12).backgroundColor(#fff).borderRadius(8).width(100%) .border({ width: 1, color: #e0e0e0 }).alignItems(HorizontalAlign.Start) // Chip 标签 Column({ space: 12 }) { Text(二、Chip 标签可关闭 / 可选中).fontSize(15).fontWeight(FontWeight.Bold).fontColor(#333) Text(关闭标签点 × 删除).fontSize(13).fontColor(#555) Column({ space: 8 }) { Row({ space: 8 }) { ForEach(this.chips.slice(0, 3), (chip: string, idx: number) { Row({ space: 4 }) { Text(chip).fontSize(13).fontColor(#0066ff) Text(×).fontSize(14).fontColor(#0066ff).fontWeight(FontWeight.Bold) .onClick(() { this.chips this.chips.filter((_: string, i: number) i ! idx) this.chipActive this.chipActive.filter((_: boolean, i: number) i ! idx) }) } .padding({ left: 10, right: 8, top: 5, bottom: 5 }) .backgroundColor(#e8f0ff).borderRadius(16).border({ width: 1, color: #b3d1ff }) }) } Row({ space: 8 }) { ForEach(this.chips.slice(3), (chip: string, relIdx: number) { Row({ space: 4 }) { Text(chip).fontSize(13).fontColor(#0066ff) Text(×).fontSize(14).fontColor(#0066ff).fontWeight(FontWeight.Bold) .onClick(() { this.chips this.chips.filter((_: string, i: number) i ! relIdx 3) this.chipActive this.chipActive.filter((_: boolean, i: number) i ! relIdx 3) }) } .padding({ left: 10, right: 8, top: 5, bottom: 5 }) .backgroundColor(#e8f0ff).borderRadius(16).border({ width: 1, color: #b3d1ff }) }) } } if (this.chips.length 0) { Button(重置标签).fontSize(13).height(38).backgroundColor(#0066ff).fontColor(#fff) .onClick(() { this.chips [ArkTS, HarmonyOS, ArkUI, NEXT, 鸿蒙] this.chipActive [true, false, true, false, false] }) } Text(筛选标签点击切换).fontSize(13).fontColor(#555) Row({ space: 8 }) { ForEach(this.chips, (chip: string, idx: number) { Text(chip).fontSize(13) .fontColor(this.chipActive[idx] ? #fff : #0066ff) .padding({ left: 12, right: 12, top: 6, bottom: 6 }) .backgroundColor(this.chipActive[idx] ? #0066ff : #e8f0ff) .borderRadius(16).border({ width: 1, color: #0066ff }) .onClick(() { const arr this.chipActive.slice() arr[idx] !arr[idx] this.chipActive arr }) }) } } .padding(12).backgroundColor(#fff).borderRadius(8).width(100%) .border({ width: 1, color: #e0e0e0 }).alignItems(HorizontalAlign.Start) } .padding(20).width(100%) } .width(100%).height(100%).backgroundColor(#f5f5f5) } }API 速查组件/属性说明Badge({ count, maxCount, position, style })数字徽标count0 自动隐藏Badge({ value, position, style })文字徽标‘NEW’、‘HOT’ 等BadgePosition.RightTop / Right / Left徽标位置style.badgeSize徽标圆圈大小style.badgeColor徽标背景色小结Badge count0 自动隐藏不需要用if条件判断是否显示 BadgeArkUI 没有内置 Chip 组件6.1 部分版本用Row Text Text(×)可以完美模拟Row 没有 flexWrap多行标签需要手动拆 Row或改用 Flex 组件ForEach UI 回调内不能声明 const需要在回调内计算的索引用表达式relIdx 3内联上一篇Select 与 TextPicker 选择器 | 下一篇RichEditor 富文本编辑器内联样式与 Span 管理

相关新闻

HarmonyOS ArkUI Select 与 TextPicker 选择器:下拉、滚筒与省市联动

HarmonyOS ArkUI Select 与 TextPicker 选择器:下拉、滚筒与省市联动

系列:鸿蒙 HarmonyOS 6.1 新特性实战 第 38 篇 Select(下拉选择器)和 TextPicker(滚筒选择器)是两种互补的选择组件:Select 适合选项多、界面紧凑的场景;TextPicker 适合连续值或较少选项的滚动…

2026/7/18 22:16:51阅读更多 →
2026年半封闭丝杆模组怎么选?哪家才是你的最佳之选?

2026年半封闭丝杆模组怎么选?哪家才是你的最佳之选?

在工业自动化飞速发展的今天,半封闭丝杆模组作为关键部件,其性能直接影响着生产效率和产品质量。面对市场上众多的品牌和型号,该如何做出选择呢?接下来,让我们一起探讨一下。精度性能:启英模组更胜一筹很多…

2026/7/18 22:14:50阅读更多 →
【无人机控制】基于MATLAB模拟六自由度四旋翼无人飞行器(UAV)的飞行动力学与控制

【无人机控制】基于MATLAB模拟六自由度四旋翼无人飞行器(UAV)的飞行动力学与控制

✅作者简介:热爱科研的Matlab仿真开发者,擅长毕业设计辅导、数学建模、数据处理、算法改进、程序设计科研仿真。🍎完整代码获取 定制创新 论文复现私信🍊个人信条:做科研,博学之、审问之、慎思之、明辨之、…

2026/7/18 22:14:50阅读更多 →
EasyMarkets:面向成熟用户的综合服务评估

EasyMarkets:面向成熟用户的综合服务评估

EasyMarkets:面向成熟用户的综合服务评估选择经纪商类平台时,用户往往会同时考虑品牌认知、平台秩序、资料说明和后续服务。围绕EasyMarkets做评测,适合把重点放在长期使用感受上,而不是用夸张词汇制造印象。一个平台若能在关键流…

2026/7/18 23:49:03阅读更多 →
β角概念及仿真方法及变化特色

β角概念及仿真方法及变化特色

在航天器轨道热控、能源平衡(太阳能帆板发电效率)以及光电载荷的链路预算中,β\betaβ 角(Beta Angle) 是一个至关重要的几何参数。 下面为你系统地拆解 β\betaβ 角的物理定义、基于轨道六根数的数学计算方法,以及如何在 STK 中进行快速仿真与数据提取。

2026/7/18 23:49:03阅读更多 →
【限时公开】头部MCN内部BGM策略库:237条已验证高完播率AI音乐标签体系+平台推荐权重映射表

【限时公开】头部MCN内部BGM策略库:237条已验证高完播率AI音乐标签体系+平台推荐权重映射表

更多请点击: https://kaifayun.com 第一章:AI音乐短视频热门BGM的底层逻辑与平台算法耦合机制 AI驱动的BGM推荐并非简单匹配音频特征,而是深度嵌入平台内容分发体系的闭环反馈系统。其核心在于“听觉信号—用户行为—流量权重”三者的实时耦…

2026/7/18 23:49:03阅读更多 →
ChatGPT写亲情故事总显生硬?神经语言学证实:缺这1个“记忆锚点”就注定失败

ChatGPT写亲情故事总显生硬?神经语言学证实:缺这1个“记忆锚点”就注定失败

更多请点击: https://kaifayun.com 第一章:ChatGPT写亲情故事总显生硬?神经语言学证实:缺这1个“记忆锚点”就注定失败 人类叙事能力根植于具身记忆——fMRI研究显示,当人回忆真实亲情场景时,海马体与前额…

2026/7/18 23:49:03阅读更多 →
瑞德克斯的资料流程会更安心吗?

瑞德克斯的资料流程会更安心吗?

看瑞德克斯时,很多人会先关心账户安全、资料流程和规则边界是否讲得细。从规则边界角度观察,平台把复杂事项拆解得更容易理解,使用者自然更容易形成稳定印象。因此,文章如果从场景、说明和服务边界展开,会比空泛称赞更…

2026/7/18 23:49:03阅读更多 →
HNU 湖南大学 系统编程与创新设计 2026BSP中期验收

HNU 湖南大学 系统编程与创新设计 2026BSP中期验收

1. (论述题, 100.0 分)步进电机: 1号电机正转40步(速度:每秒10步) 震动:震动后,蜂鸣器发出2次“叮咚”声(叮1.2KHz,咚500Hz)。#include "STC15F2K60S2.H" #include "sys.H" #include "StepMotor.h"code unsigned long S…

2026/7/18 23:47:03阅读更多 →
VSCode TypeScript 环境配置对比:全局安装 vs 项目本地安装的4个关键差异

VSCode TypeScript 环境配置对比:全局安装 vs 项目本地安装的4个关键差异

VSCode TypeScript 环境配置对比:全局安装 vs 项目本地安装的4个关键差异当你在VSCode中启动一个新的TypeScript项目时,第一个技术决策往往从安装方式开始。这个看似简单的选择——全局安装还是项目本地安装——实际上会深刻影响你的开发流程、团队协作和…

2026/7/18 10:49:13阅读更多 →
智慧树刷课插件:5分钟实现自动化学习的智能助手

智慧树刷课插件:5分钟实现自动化学习的智能助手

智慧树刷课插件:5分钟实现自动化学习的智能助手 【免费下载链接】zhihuishu 智慧树刷课插件,自动播放下一集、1.5倍速度、无声 项目地址: https://gitcode.com/gh_mirrors/zh/zhihuishu 智慧树刷课插件是一款专为智慧树在线教育平台设计的Chrome浏…

2026/7/18 8:49:08阅读更多 →
Steam创意工坊下载器WorkshopDL:跨平台游戏模组获取的终极解决方案

Steam创意工坊下载器WorkshopDL:跨平台游戏模组获取的终极解决方案

Steam创意工坊下载器WorkshopDL:跨平台游戏模组获取的终极解决方案 【免费下载链接】WorkshopDL WorkshopDL - The Best Steam Workshop Downloader 项目地址: https://gitcode.com/gh_mirrors/wo/WorkshopDL 你是否在GOG或Epic Games Store购买了心仪的游戏…

2026/7/18 14:49:24阅读更多 →
从模糊意图到可执行指令:Claude PRD中Prompt Engineering与需求颗粒度的5级映射法则

从模糊意图到可执行指令:Claude PRD中Prompt Engineering与需求颗粒度的5级映射法则

更多请点击: https://kaifayun.com 第一章:从模糊意图到可执行指令:Claude PRD中Prompt Engineering与需求颗粒度的5级映射法则 在Claude驱动的产品需求文档(PRD)生成实践中,原始业务意图往往以自然语言片…

2026/7/18 0:00:14阅读更多 →
Cursor配置生成失效?3大隐藏陷阱+4行修复代码,资深工程师连夜整理的紧急补救清单

Cursor配置生成失效?3大隐藏陷阱+4行修复代码,资深工程师连夜整理的紧急补救清单

更多请点击: https://codechina.net 第一章:Cursor配置生成失效?3大隐藏陷阱4行修复代码,资深工程师连夜整理的紧急补救清单 Cursor 配置生成突然失效,是近期高频报障场景。表面看是 cursor.config.json 未更新或 LSP…

2026/7/18 0:00:14阅读更多 →
某智驾大牛创业

某智驾大牛创业

作者:钟声编辑:Mark出品:红色星际头图:智能驾驶图片据悉,国内某头部智驾公司端到端模型技术大牛Z投身创业,并且已经拿到融资。Z不仅是该头部公司内部最年轻的对标阿里P10级别技术负责⼈,更是业内…

2026/7/18 0:00:14阅读更多 →
YOLOv8推理性能优化:从1.2FPS到35FPS的全链路加速实践

YOLOv8推理性能优化:从1.2FPS到35FPS的全链路加速实践

如果你在部署 YOLOv8 时,发现推理速度只有可怜的 1-2 FPS,而别人的演示视频却能跑到 30 FPS 以上,那么问题很可能不在模型本身,而在于你的整个处理链路。很多开发者拿到一个训练好的 YOLOv8 模型后,会直接使用官方示例…

2026/7/18 22:49:46阅读更多 →
Coze与Dify对比指南:低代码AI应用开发从入门到实战

Coze与Dify对比指南:低代码AI应用开发从入门到实战

1. 从零到一:为什么你需要了解 Coze 和 Dify?如果你对 AI 应用开发感兴趣,但一看到“大模型”、“智能体”、“工作流”这些词就头疼,觉得门槛太高,那这篇文章就是为你准备的。很多开发者,包括我自己&#…

2026/7/18 14:49:24阅读更多 →
AI生图工具怎么选?2026年6月版实测对比

AI生图工具怎么选?2026年6月版实测对比

做自媒体的朋友应该都有体会:配图一直是个让人头疼的问题。2026年,AI生图工具已经非常成熟了,但工具太多反而不知道怎么选。以下是截至2026年6月我对主流AI生图工具的实测对比。Midjourney V8.1:速度之王2026年6月11日&#xff0c…

2026/7/18 18:49:35阅读更多 →