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/8/10 13:42:54 阅读更多 →
日志系统的设计:从 console.log 到结构化日志

日志系统的设计:从 console.log 到结构化日志

日志系统的设计:从 console.log 到结构化日志 一、console.log 在什么时候不够用 开发阶段,console.log 是最直接的调试工具。打印请求参数、输出中间结果,在本地跑一下就知道发生了什么。这个习惯在部署到生产环境后,往往延续下来——生产代码里依然充斥着 console…

2026/8/12 23:07:57 阅读更多 →
AI外贸获客系统:企业如何利用AI提升海外客户开发效率?

AI外贸获客系统:企业如何利用AI提升海外客户开发效率?

随着全球贸易环境不断变化,外贸企业正在寻找更加高效的客户开发方式。过去,外贸企业主要依靠人工完成客户开发流程。销售人员寻找目标市场。销售人员收集客户资料。销售人员联系潜在客户。销售人员持续跟进合作机会。这种方式长期以来发挥了重要作用。但…

2026/8/13 12:26:15 阅读更多 →

最新新闻

空洞骑士模组管理器 Lumafly 使用指南:三步告别依赖地狱,一键畅玩300+模组

空洞骑士模组管理器 Lumafly 使用指南:三步告别依赖地狱,一键畅玩300+模组

空洞骑士模组管理器 Lumafly 使用指南:三步告别依赖地狱,一键畅玩300模组 【免费下载链接】Lumafly A cross platform mod manager for Hollow Knight written in Avalonia. 项目地址: https://gitcode.com/gh_mirrors/lu/Lumafly 你有没有过这样…

2026/8/15 22:49:32 阅读更多 →
第四篇 STM32MP157-M4:Makefile 完整详解

第四篇 STM32MP157-M4:Makefile 完整详解

STM32MP157-M4:Makefile 完整逐段详解【高危前置提醒】必须开启 --gc-sections 死代码回收避免 SRAM 溢出;编译参数必须携带 -DCORE_CM4;os_config/ 必须在 -I 列表首行。0 前言 纯 GNU Make 构建工程,不依赖任何 IDE。Makefile 自…

2026/8/15 22:49:32 阅读更多 →
YOLOv8 飞鸟智能预警全栈工程|单类别 VOC/YOLO 航拍数据集、PyQt5 精美 GUI、ONNX 量化推理、全套训练评估曲线落地

YOLOv8 飞鸟智能预警全栈工程|单类别 VOC/YOLO 航拍数据集、PyQt5 精美 GUI、ONNX 量化推理、全套训练评估曲线落地

目录 一、前言 二、5416 张单类别飞鸟航拍数据集完整解析 2.1 数据集基础完整参数 2.2 飞鸟检测数据集专属优势 2.3 数据集固有短板与配套涨点方案 三、YOLOv8 飞鸟检测专属涨点核心原理 四、三大行业落地应用案例 案例 1 民航机场净空飞鸟预警系统 业务痛点机场跑道周…

2026/8/15 22:49:32 阅读更多 →
YOLOv8 八类人脸表情识别全栈工程|8038 张 VOC/YOLO 数据集、PyQt5 精美 GUI、ONNX 量化推理、全套训练评估曲线落地

YOLOv8 八类人脸表情识别全栈工程|8038 张 VOC/YOLO 数据集、PyQt5 精美 GUI、ONNX 量化推理、全套训练评估曲线落地

目录 一、前言 二、8038 张八类人脸表情数据集完整解析 2.1 数据集基础完整参数 2.2 人脸表情数据集专属优势 2.3 数据集固有短板与配套涨点优化策略 三、YOLOv8 人脸表情专属涨点核心原理 四、四大行业落地应用案例 案例 1 智慧课堂学情实时分析系统 业务痛点教师无法…

2026/8/15 22:49:32 阅读更多 →
AI智能体在制造业的应用:从工艺知识传承到实时质量控制

AI智能体在制造业的应用:从工艺知识传承到实时质量控制

1. 项目概述:当AI智能体走进工厂车间最近和几个在制造业干了十几年的老工程师聊天,大家聊得最多的不是新设备,而是“人”。不是招不到人,而是留不住人,更留不住“经验”。一个老师傅退休,带走的可能是一整套…

2026/8/15 22:49:32 阅读更多 →
深入骨髓!50行代码拆解LLM Harness工程:如何用“赛马机制”根治幻觉

深入骨髓!50行代码拆解LLM Harness工程:如何用“赛马机制”根治幻觉

别把Harness当成简单的“调三次API取平均”。这50行代码里,藏着并发锁、隐式容错、评分注入防御和流量控制。读懂它,你就算摸到了AI工程化的入门门槛。很多同学跑过这段代码后,觉得不过如此:“哦,就是并发生成三个结果…

2026/8/15 22:48:32 阅读更多 →

日新闻

内景 空间站内部 中国空间站 太空 内仓

内景 空间站内部 中国空间站 太空 内仓

本项目为前几天收费帮学妹做的一个项目,在工作环境中基本使用不到,但是很多学校把这个当作编程入门的项目来做,故分享出本项目供初学者参考。 一、项目描述 空间站内部 中国空间站 太空 内仓 地址:本地PC端运行(或Web…

2026/8/15 0:00:30 阅读更多 →
重新定义数据接口:3个突破性场景让通达信数据读取更智能

重新定义数据接口:3个突破性场景让通达信数据读取更智能

重新定义数据接口:3个突破性场景让通达信数据读取更智能 【免费下载链接】mootdx 通达信数据读取的一个简便使用封装 项目地址: https://gitcode.com/GitHub_Trending/mo/mootdx 当我们面对海量金融数据时,传统的数据获取方式往往让我们陷入困境—…

2026/8/15 0:00:30 阅读更多 →
一文读懂快消WMS怎么选?2026年国内外10大主流WMS品牌盘点

一文读懂快消WMS怎么选?2026年国内外10大主流WMS品牌盘点

快消品(FMCG)是流通速度较快、竞争较为激烈的行业之一。一瓶饮料从出厂到消费者手中,往往只有几十天甚至几天的周转窗口。这决定了快消行业的仓储管理系统(WMS)与制造业、电商行业存在明显区别:它不仅需要管…

2026/8/15 0:02:30 阅读更多 →

周新闻

5分钟告别提取码焦虑:baidupankey如何智能破解百度网盘资源锁

5分钟告别提取码焦虑:baidupankey如何智能破解百度网盘资源锁

5分钟告别提取码焦虑:baidupankey如何智能破解百度网盘资源锁 【免费下载链接】baidupankey 在线查询网盘提取码(维护中 rm repo) 项目地址: https://gitcode.com/gh_mirrors/ba/baidupankey 你是否曾经在深夜寻找一份重要资料&#x…

2026/8/13 2:38:34 阅读更多 →
如何快速生成中国车牌图片:Python开源工具完整指南

如何快速生成中国车牌图片:Python开源工具完整指南

如何快速生成中国车牌图片:Python开源工具完整指南 【免费下载链接】chinese_license_plate_generator 中国车牌生成器 项目地址: https://gitcode.com/gh_mirrors/ch/chinese_license_plate_generator 中国车牌生成器是一个基于Python的开源项目&#xff0c…

2026/8/15 12:59:14 阅读更多 →
收藏!小白程序员轻松入门大模型,从Harness工程开始实践

收藏!小白程序员轻松入门大模型,从Harness工程开始实践

文章强调学习大模型不应只关注模型本身,而应重视模型外的系统搭建,即Harness。提出AgentModelHarness的实用公式,详细介绍Harness的四个层次:持久化层、执行层、控制层和观察与验证层。文章还探讨了上下文工程、工具设计、AGENTS.…

2026/8/13 10:41:51 阅读更多 →

月新闻

免费解锁百度网盘SVIP加速:macOS用户必备的下载提速终极指南

免费解锁百度网盘SVIP加速:macOS用户必备的下载提速终极指南

免费解锁百度网盘SVIP加速:macOS用户必备的下载提速终极指南 【免费下载链接】BaiduNetdiskPlugin-macOS For macOS.百度网盘 破解SVIP、下载速度限制~ 项目地址: https://gitcode.com/gh_mirrors/ba/BaiduNetdiskPlugin-macOS 还在为百度网盘macOS版的龟速下…

2026/8/14 13:40:53 阅读更多 →
终极ncmdump指南:3分钟实现网易云NCM音乐解密与格式转换

终极ncmdump指南:3分钟实现网易云NCM音乐解密与格式转换

终极ncmdump指南:3分钟实现网易云NCM音乐解密与格式转换 【免费下载链接】ncmdump 项目地址: https://gitcode.com/gh_mirrors/ncmd/ncmdump 还在为网易云音乐下载的NCM格式文件无法在其他播放器播放而烦恼吗?ncmdump解密工具帮你轻松解决这个困…

2026/8/14 14:06:45 阅读更多 →
HarmonyOS 应用开发《掌上英语》第81篇: 智能体卡片:为英语学习 App 打造桌面级学习助手

HarmonyOS 应用开发《掌上英语》第81篇: 智能体卡片:为英语学习 App 打造桌面级学习助手

AgentCard 智能体卡片:为英语学习 App 打造桌面级学习助手适用平台:HarmonyOS 7.0 (API 26 Beta)一、引言 HarmonyOS 7.0(API 26 Beta)新增了 AgentCard 智能体卡片能力,这是继 HMAF(鸿蒙智能体框架&#x…

2026/8/15 2:35:29 阅读更多 →