温馨提示:本篇博客的详细代码已发布到 git : https://gitcode.com/nutpi/HarmonyosNext 可以下载运行哦!
Tag组件:轻量级标签展示,用于标记与分类。本教程将指导您如何在HarmonyOS NEXT中实现仿UV-UI的Tag组件。
属性名 | 类型 | 默认值 | 说明 |
---|---|---|---|
text | string | - | 标签显示的文本内容 |
type | string | 'default' | 标签类型,可选值:default/primary/success/warning/danger |
closable | boolean | false | 是否可关闭 |
disabled | boolean | false | 是否禁用 |
icon | string | Resource | - | 图标资源 |
// 基础标签示例
Flex({ wrap: FlexWrap.Wrap }) {
Tag({ text: '默认标签' })
Tag({ text: '主要标签', type: 'primary' })
Tag({ text: '成功标签', type: 'success' })
Tag({ text: '警告标签', type: 'warning' })
Tag({ text: '危险标签', type: 'danger' })
}
Tag组件内置了一套完整的颜色系统,包括:
// 颜色配置示例
private getTextColor(): string {
const colors = {
default: '#333333',
primary: '#2468f2',
success: '#18a058',
warning: '#f0a020',
danger: '#d03050'
}
return colors[this.type] || colors.default
}
Tag组件支持灵活的布局配置:
Row() {
// 图标布局
if (this.icon) {
Image(this.icon)
.width(16)
.height(16)
.margin({ right: 4 })
}
// 文本布局
Text(this.text)
.fontSize(14)
.fontColor(this.getTextColor())
}
.padding(8)
.borderRadius(4)
Tag({
text: '可关闭标签',
closable: true,
onCloseTag: () => {
console.info('标签被关闭')
}
})
Tag({
text: '带图标标签',
type: 'primary',
icon: $r('app.media.startIcon')
})
Tag({
text: '禁用标签',
type: 'warning',
disabled: true
})
下一篇教程将介绍Tag组件的进阶特性和高级用法,敬请期待!
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。