天气药丸标签:Row + Text 组合实现品牌药丸

前言

药丸标签(Pill Badge)是移动应用中常见的 UI 元素,用于展示状态或分类信息。"海风日记"在首页的日期行中使用了天气药丸标签,展示当前天气状况和 emoji,通过 Row + Text 组合实现。

本文将从源码出发,深入讲解药丸标签的设计和实现,以及如何通过 border、backgroundColor 和 borderRadius 的组合实现品牌药丸样式。


一、天气药丸的实现

1.1 源码

typescript 复制代码
Row({ space: 3 }) {
  Text('☀️').fontSize(12)                               // 天气 emoji
  Text('晴天').fontSize(12).fontColor('#C8800A')         // 天气文字
    .fontWeight(FontWeight.Medium)
}
.backgroundColor('rgba(245,166,35,0.12)')                // 浅橙色背景
.border({ width: 1, color: 'rgba(245,166,35,0.22)' })  // 橙色边框
.borderRadius(10)                                         // 圆角
.padding({ left: 8, right: 8, top: 2, bottom: 2 })       // 紧凑内边距

1.2 视觉样式分析

属性 效果
backgroundColor rgba(245,166,35,0.12) 12% 透明度橙色背景
border.width 1 细边框
border.color rgba(245,166,35,0.22) 22% 透明度橙色边框
borderRadius 10 药丸圆角
padding 8/8/2/2 水平宽、垂直窄

二、药丸标签的通用设计

2.1 标签样式模板

typescript 复制代码
// 通用药丸标签构建器
@Builder
pillTag(emoji: string, text: string, color: string) {
  Row({ space: 3 }) {
    Text(emoji).fontSize(12)
    Text(text).fontSize(12).fontColor(color).fontWeight(FontWeight.Medium)
  }
  .backgroundColor(`${color}1A`)  // 约 10% 透明度
  .border({ width: 1, color: `${color}33` })  // 约 20% 透明度
  .borderRadius(10)
  .padding({ left: 8, right: 8, top: 2, bottom: 2 })
}

2.2 不同颜色标签

typescript 复制代码
// 橙色标签(天气)
this.pillTag('☀️', '晴天', '#C8800A')

// 蓝色标签(心情)
this.pillTag('😊', '开心', '#5A9EFF')

// 绿色标签(状态)
this.pillTag('✅', '完成', '#4CAF50')

三、标签在项目中的使用

3.1 日记详情标签

typescript 复制代码
Text(`# ${tag}`)
  .fontSize(12).fontColor(COLOR_AMBER)
  .backgroundColor('rgba(245,166,35,0.08)')
  .padding({ left: 8, right: 8, top: 3, bottom: 3 })
  .borderRadius(8)

3.2 日历节假日标签

typescript 复制代码
Text('国庆假期')
  .fontSize(11).fontColor(COLOR_PRIMARY).fontWeight(FontWeight.Bold)
  .backgroundColor('rgba(245,166,35,0.15)')
  .padding({ left: 8, right: 8, top: 3, bottom: 3 })
  .borderRadius(8)
  .border({ width: 1, color: 'rgba(245,166,35,0.25)' })

四、常见问题与排查

4.1 标签文字颜色不清晰

问题:标签文字颜色与背景对比度不足。

解决方案 :使用 fontWeight(FontWeight.Medium)FontWeight.Bold 增加文字厚度。

4.2 标签大小不一致

问题:不同位置的标签大小不一致。

解决方案 :统一使用 paddingfontSize 控制标签尺寸。


总结

本文通过"海风日记"的天气药丸标签,深入讲解了药丸标签的实现:

  1. Row + Text 组合:emoji 和文字的水平排列
  2. 透明度颜色:backgroundColor 和 border 的透明度控制
  3. 通用模板:可复用的药丸标签构建器
  4. 多场景应用:天气、标签、节假日等不同场景

至此,第三系列"首页与日记卡片"全部完成 !从下一篇文章开始进入 第四系列:写日记与编辑

如果这篇文章对你有帮助,欢迎点赞👍、收藏⭐、关注🔔,你的支持是我持续创作的动力!


相关资源:

相关推荐
GetcharZp32 分钟前
5 分钟拥有自己的 S3:RustFS 上手(MinIO 的 Rust 替代,Apache 2.0 可商用)
后端
行百里er1 小时前
Redis 版本演进、新特性与协议那些事儿
redis·后端
Joy T2 小时前
Spring AI 2.0 Agent 进阶:Memory、State 与 Context Engineering 常见技术全景
java·人工智能·后端·spring·agent入门·agent state
打工仔折腾 AI2 小时前
FastAPI 从本机到生产服务器:Nginx+Gunicorn+Uvicorn 完整部署实录
人工智能·后端·python·nginx·fastapi·gunicorn
IT_陈寒2 小时前
Java空指针这次真把我坑惨了
前端·人工智能·后端
moMo3 小时前
LangChain 到 LangGraph: RAG 知识库改造
后端
用户574385305113 小时前
元数据驱动的通用 CRUD 后端框架 (3- createBusiness 工厂:三层装配 + 一行出 REST)
后端
高级程序源3 小时前
django大学生创新创业项目管理系统94923-计算机课程设计、毕业设计
javascript·vue.js·spring boot·后端·python·django·课程设计
Sam_Deep_Thinking4 小时前
new Thread()之后发生了什么?
java·后端·面试·程序员