• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Badge
2
3The **\<Badge>** component is a container that can be attached to another component for tagging.
4
5>  **NOTE**
6>
7> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
8
9
10## Child Components
11
12This component supports only one child component.
13
14>  **NOTE**
15>
16>  Built-in components and custom components are allowed, with support for ([if/else](../../quick-start/arkts-rendering-control-ifelse.md), [ForEach](../../quick-start/arkts-rendering-control-foreach.md), and [LazyForEach](../../quick-start/arkts-rendering-control-lazyforeach.md)) rendering control.
17
18
19## APIs
20
21### Badge
22
23Badge(value: {count: number, position?: BadgePosition \| Position, maxCount?: number, style: BadgeStyle})
24
25Creates a badge.
26
27Since API version 9, this API is supported in ArkTS widgets.
28
29**Parameters**
30
31| Name| Type| Mandatory| Description|
32| -------- | -------- | -------- | -------- |
33| count | number | Yes| Number of notifications.<br>**NOTE**<br>If the value is less than or equal to 0, no badge is displayed.<br>Value range: [-2147483648, 2147483647]<br>If the value is not an integer, it is rounded off to the nearest integer. For example, 5.5 is rounded off to 5.|
34| position | [BadgePosition](#badgeposition)\|[Position<sup>10+</sup>](ts-types.md#position8) | No| Position to display the badge relative to the parent component.<br>Default value: **BadgePosition.RightTop**<br>**NOTE**<br> This parameter cannot be set in percentage. If it is set to an invalid value, the default value **(0,0)** will be used.|
35| maxCount | number | No| Maximum number of notifications. When the maximum number is reached, only **maxCount+** is displayed.<br>Default value: **99**<br>Value range: [-2147483648, 2147483647]<br>If the value is not an integer, it is rounded off to the nearest integer. For example, 5.5 is rounded off to 5.|
36| style | [BadgeStyle](#badgestyle) | Yes| Style of the badge, including the font color, font size, badge color, and badge size.|
37
38### Badge
39
40Badge(value: {value: string, position?: BadgePosition \| Position, style: BadgeStyle})
41
42Creates a badge based on the given string.
43
44Since API version 9, this API is supported in ArkTS widgets.
45
46**Parameters**
47
48| Name| Type| Mandatory| Default Value| Description|
49| -------- | -------- | -------- | -------- | -------- |
50| value | string | Yes| - | Prompt content.|
51| position | [BadgePosition](#badgeposition)\|[Position<sup>10+</sup>](ts-types.md#position8) | No| BadgePosition.RightTop | Position to display the badge relative to the parent component.|
52| style | [BadgeStyle](#badgestyle) | Yes| - | Style of the badge, including the font color, font size, badge color, and badge size.|
53
54## BadgePosition
55
56Since API version 9, this API is supported in ArkTS widgets.
57
58| Name| Description|
59| -------- | -------- |
60| RightTop | The badge is displayed in the upper right corner of the parent component.|
61| Right | The badge is vertically centered on the right of the parent component.|
62| Left | The badge is vertically centered on the left of the parent component.|
63
64## BadgeStyle
65
66Since API version 9, this API is supported in ArkTS widgets.
67
68| Name                     | Type                                                        | Mandatory| Description                                                        |
69| ------------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
70| color                     | [ResourceColor](ts-types.md#resourcecolor)                   | No  | Font color.<br>Default value: **Color.White**                          |
71| fontSize                  | number \| string                                   | No  | Font size.<br>Default value: **10**<br>Unit: vp<br>**NOTE**<br>This parameter cannot be set in percentage.|
72| badgeSize                 | number \| string                                   | No  | Badge size.<br>Default value: **16**<br>Unit: vp<br>**NOTE**<br>This parameter cannot be set in percentage. If it is set to an invalid value, the default value is used.|
73| badgeColor                | [ResourceColor](ts-types.md#resourcecolor)                   | No  | Badge color.<br>Default value: **Color.Red**                         |
74| fontWeight<sup>10+</sup>  | number \|[FontWeight](ts-appendix-enums.md#fontweight) \| string | No  | Font weight of the text.<br>Default value: **FontWeight.Normal**<br>**NOTE**<br>This parameter cannot be set in percentage.|
75| borderColor<sup>10+</sup> | [ResourceColor](ts-types.md#resourcecolor)                   | No  | Border color of the background.                                              |
76| borderWidth<sup>10+</sup> | [Length](ts-types.md#length)                                 | No  | Border width of the background.<br>Default value: **1**<br>Unit: vp<br>**NOTE**<br>This parameter cannot be set in percentage.|
77
78## Attributes
79
80The [universal attributes](ts-universal-attributes-size.md) are supported.
81
82## Events
83
84The [universal events](ts-universal-events-click.md) are supported.
85
86## Example
87
88```ts
89// xxx.ets
90@Entry
91@Component
92struct BadgeExample {
93  @Builder TabBuilder(index: number) {
94    Column() {
95      if (index === 2) {
96        Badge({
97          value: '',
98          style: { badgeSize: 6, badgeColor: '#FA2A2D' }
99        }) {
100          Image('/common/public_icon_off.svg')
101            .width(24)
102            .height(24)
103        }
104        .width(24)
105        .height(24)
106        .margin({ bottom: 4 })
107      } else {
108        Image('/common/public_icon_off.svg')
109          .width(24)
110          .height(24)
111          .margin({ bottom: 4 })
112      }
113      Text('Tab')
114        .fontColor('#182431')
115        .fontSize(10)
116        .fontWeight(500)
117        .lineHeight(14)
118    }.width('100%').height('100%').justifyContent(FlexAlign.Center)
119  }
120
121  @Builder itemBuilder(value: string) {
122    Row() {
123      Image('common/public_icon.svg').width(32).height(32).opacity(0.6)
124      Text(value)
125        .width(177)
126        .height(21)
127        .margin({ left: 15, right: 76 })
128        .textAlign(TextAlign.Start)
129        .fontColor('#182431')
130        .fontWeight(500)
131        .fontSize(16)
132        .opacity(0.9)
133      Image('common/public_icon_arrow_right.svg').width(12).height(24).opacity(0.6)
134    }.width('100%').padding({ left: 12, right: 12 }).height(56)
135  }
136
137  build() {
138    Column() {
139      Text('dotsBadge').fontSize(18).fontColor('#182431').fontWeight(500).margin(24)
140      Tabs() {
141        TabContent()
142          .tabBar(this.TabBuilder(0))
143        TabContent()
144          .tabBar(this.TabBuilder(1))
145        TabContent()
146          .tabBar(this.TabBuilder(2))
147        TabContent()
148          .tabBar(this.TabBuilder(3))
149      }
150      .width(360)
151      .height(56)
152      .backgroundColor('#F1F3F5')
153
154      Column() {
155        Text('stringBadge').fontSize(18).fontColor('#182431').fontWeight(500).margin(24)
156        List({ space: 12 }) {
157          ListItem() {
158            Text('list1').fontSize(14).fontColor('#182431').margin({ left: 12 })
159          }
160          .width('100%')
161          .height(56)
162          .backgroundColor('#FFFFFF')
163          .borderRadius(24)
164          .align(Alignment.Start)
165
166          ListItem() {
167            Badge({
168              value: 'New',
169              position: BadgePosition.Right,
170              style: { badgeSize: 16, badgeColor: '#FA2A2D' }
171            }) {
172              Text('list2').width(27).height(19).fontSize(14).fontColor('#182431')
173            }.width(49.5).height(19)
174            .margin({ left: 12 })
175          }
176          .width('100%')
177          .height(56)
178          .backgroundColor('#FFFFFF')
179          .borderRadius(24)
180          .align(Alignment.Start)
181        }.width(336)
182
183        Text('numberBadge').fontSize(18).fontColor('#182431').fontWeight(500).margin(24)
184        List() {
185          ListItem() {
186            this.itemBuilder('list1')
187          }
188
189          ListItem() {
190            Row() {
191              Image('common/public_icon.svg').width(32).height(32).opacity(0.6)
192              Badge({
193                count: 1,
194                position: BadgePosition.Right,
195                style: { badgeSize: 16, badgeColor: '#FA2A2D' }
196              }) {
197                Text('list2')
198                  .width(177)
199                  .height(21)
200                  .textAlign(TextAlign.Start)
201                  .fontColor('#182431')
202                  .fontWeight(500)
203                  .fontSize(16)
204                  .opacity(0.9)
205              }.width(240).height(21).margin({ left: 15, right: 11 })
206
207              Image('common/public_icon_arrow_right.svg').width(12).height(24).opacity(0.6)
208            }.width('100%').padding({ left: 12, right: 12 }).height(56)
209          }
210
211          ListItem() {
212            this.itemBuilder('list3')
213          }
214
215          ListItem() {
216            this.itemBuilder('list4')
217          }
218        }
219        .width(336)
220        .height(232)
221        .backgroundColor('#FFFFFF')
222        .borderRadius(24)
223        .padding({ top: 4, bottom: 4 })
224        .divider({ strokeWidth: 0.5, color: 'rgba(0,0,0,0.1)', startMargin: 60, endMargin: 12 })
225      }.width('100%').backgroundColor('#F1F3F5').padding({ bottom: 12 })
226    }.width('100%')
227  }
228}
229```
230
231![badge](figures/badge.png)
232