1# Badge 2 3可以附加在单个组件上用于信息标记的容器组件。 4 5> **说明:** 6> 7> 该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8 9 10## 子组件 11 12支持单个子组件。 13 14> **说明:** 15> 16> 子组件类型:系统组件和自定义组件,支持渲染控制类型([if/else](../../quick-start/arkts-rendering-control-ifelse.md)、[ForEach](../../quick-start/arkts-rendering-control-foreach.md)和[LazyForEach](../../quick-start/arkts-rendering-control-lazyforeach.md))。 17 18 19## 接口 20 21**方法1:** Badge(value: {count: number, position?: BadgePosition, maxCount?: number, style: BadgeStyle}) 22 23创建数字标记组件。 24 25从API version 9开始,该接口支持在ArkTS卡片中使用。 26 27**参数:** 28 29| 参数名 | 参数类型 | 必填 | 参数描述 | 30| -------- | -------- | -------- | -------- | 31| count | number | 是 | 设置提醒消息数。<br/>**说明:** <br/>小于等于0时不显示信息标记。<br/>取值范围:[-2147483648,2147483647],非整数时会舍去小数部分取整数部分,如5.5取5。 | 32| position | [BadgePosition](#badgeposition枚举说明) | 否 | 设置提示点显示位置。<br/>默认值:BadgePosition.RightTop | 33| maxCount | number | 否 | 最大消息数,超过最大消息时仅显示maxCount+。<br/>默认值:99<br/>取值范围:[-2147483648,2147483647],非整数时会舍去小数部分取整数部分,如5.5取5。 | 34| style | [BadgeStyle](#badgestyle对象说明) | 是 | Badge组件可设置样式,支持设置文本颜色、尺寸、圆点颜色和尺寸。 | 35 36**方法2:** Badge(value: {value: string, position?: BadgePosition, style: BadgeStyle}) 37 38根据字符串创建标记组件。 39 40从API version 9开始,该接口支持在ArkTS卡片中使用。 41 42**参数:** 43 44| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 | 45| -------- | -------- | -------- | -------- | -------- | 46| value | string | 是 | - | 提示内容的文本字符串。 | 47| position | [BadgePosition](#badgeposition枚举说明) | 否 | BadgePosition.RightTop | 设置提示点显示位置。 | 48| style | [BadgeStyle](#badgestyle对象说明) | 是 | - | Badge组件可设置样式,支持设置文本颜色、尺寸、圆点颜色和尺寸。 | 49 50## BadgePosition枚举说明 51 52从API version 9开始,该接口支持在ArkTS卡片中使用。 53 54| 名称 | 描述 | 55| -------- | -------- | 56| RightTop | 圆点显示在右上角。 | 57| Right | 圆点显示在右侧纵向居中。 | 58| Left | 圆点显示在左侧纵向居中。 | 59 60## BadgeStyle对象说明 61 62从API version 9开始,该接口支持在ArkTS卡片中使用。 63 64| 名称 | 类型 | 必填 | 默认值 | 描述 | 65| ---------- | ------------------------------------------ | ---- | ----------- | ------------------------------------------------------------ | 66| color | [ResourceColor](ts-types.md#resourcecolor) | 否 | Color.White | 文本颜色。<br/>默认值:Color.White | 67| fontSize | number \| string | 否 | 10 | 文本大小。<br/>默认值:10<br/>单位:vp<br/>**说明:** <br/>不支持设置百分比。 | 68| badgeSize | number \| string | 否 | 16 | Badge的大小,单位vp。不支持百分比形式设置。当设置为非法值时,按照默认值处理。<br/>默认值:16<br/>单位:vp<br/>**说明:** <br/>不支持设置百分比。当设置为非法值时,按照默认值处理。 | 69| badgeColor | [ResourceColor](ts-types.md#resourcecolor) | 否 | Color.Red | Badge的颜色。<br/>默认值:Color.Red | 70## 属性 71 72支持[通用属性](ts-universal-attributes-size.md)。 73 74## 事件 75 76支持[通用事件](ts-universal-events-click.md)。 77## 示例 78 79```ts 80// xxx.ets 81@Entry 82@Component 83struct BadgeExample { 84 @Builder TabBuilder(index: number) { 85 Column() { 86 if (index === 2) { 87 Badge({ 88 value: '', 89 style: { badgeSize: 6, badgeColor: '#FA2A2D' } 90 }) { 91 Image('/common/public_icon_off.svg') 92 .width(24) 93 .height(24) 94 } 95 .width(24) 96 .height(24) 97 .margin({ bottom: 4 }) 98 } else { 99 Image('/common/public_icon_off.svg') 100 .width(24) 101 .height(24) 102 .margin({ bottom: 4 }) 103 } 104 Text('Tab') 105 .fontColor('#182431') 106 .fontSize(10) 107 .fontWeight(500) 108 .lineHeight(14) 109 }.width('100%').height('100%').justifyContent(FlexAlign.Center) 110 } 111 112 @Builder itemBuilder(value: string) { 113 Row() { 114 Image('common/public_icon.svg').width(32).height(32).opacity(0.6) 115 Text(value) 116 .width(177) 117 .height(21) 118 .margin({ left: 15, right: 76 }) 119 .textAlign(TextAlign.Start) 120 .fontColor('#182431') 121 .fontWeight(500) 122 .fontSize(16) 123 .opacity(0.9) 124 Image('common/public_icon_arrow_right.svg').width(12).height(24).opacity(0.6) 125 }.width('100%').padding({ left: 12, right: 12 }).height(56) 126 } 127 128 build() { 129 Column() { 130 Text('dotsBadge').fontSize(18).fontColor('#182431').fontWeight(500).margin(24) 131 Tabs() { 132 TabContent() 133 .tabBar(this.TabBuilder(0)) 134 TabContent() 135 .tabBar(this.TabBuilder(1)) 136 TabContent() 137 .tabBar(this.TabBuilder(2)) 138 TabContent() 139 .tabBar(this.TabBuilder(3)) 140 } 141 .width(360) 142 .height(56) 143 .backgroundColor('#F1F3F5') 144 145 Column() { 146 Text('stringBadge').fontSize(18).fontColor('#182431').fontWeight(500).margin(24) 147 List({ space: 12 }) { 148 ListItem() { 149 Text('list1').fontSize(14).fontColor('#182431').margin({ left: 12 }) 150 } 151 .width('100%') 152 .height(56) 153 .backgroundColor('#FFFFFF') 154 .borderRadius(24) 155 .align(Alignment.Start) 156 157 ListItem() { 158 Badge({ 159 value: 'New', 160 position: BadgePosition.Right, 161 style: { badgeSize: 16, badgeColor: '#FA2A2D' } 162 }) { 163 Text('list2').width(27).height(19).fontSize(14).fontColor('#182431') 164 }.width(49.5).height(19) 165 .margin({ left: 12 }) 166 } 167 .width('100%') 168 .height(56) 169 .backgroundColor('#FFFFFF') 170 .borderRadius(24) 171 .align(Alignment.Start) 172 }.width(336) 173 174 Text('numberBadge').fontSize(18).fontColor('#182431').fontWeight(500).margin(24) 175 List() { 176 ListItem() { 177 this.itemBuilder('list1') 178 } 179 180 ListItem() { 181 Row() { 182 Image('common/public_icon.svg').width(32).height(32).opacity(0.6) 183 Badge({ 184 count: 1, 185 position: BadgePosition.Right, 186 style: { badgeSize: 16, badgeColor: '#FA2A2D' } 187 }) { 188 Text('list2') 189 .width(177) 190 .height(21) 191 .textAlign(TextAlign.Start) 192 .fontColor('#182431') 193 .fontWeight(500) 194 .fontSize(16) 195 .opacity(0.9) 196 }.width(240).height(21).margin({ left: 15, right: 11 }) 197 198 Image('common/public_icon_arrow_right.svg').width(12).height(24).opacity(0.6) 199 }.width('100%').padding({ left: 12, right: 12 }).height(56) 200 } 201 202 ListItem() { 203 this.itemBuilder('list3') 204 } 205 206 ListItem() { 207 this.itemBuilder('list4') 208 } 209 } 210 .width(336) 211 .height(232) 212 .backgroundColor('#FFFFFF') 213 .borderRadius(24) 214 .padding({ top: 4, bottom: 4 }) 215 .divider({ strokeWidth: 0.5, color: 'rgba(0,0,0,0.1)', startMargin: 60, endMargin: 12 }) 216 }.width('100%').backgroundColor('#F1F3F5').padding({ bottom: 12 }) 217 }.width('100%') 218 } 219} 220``` 221 222 223