• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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### Badge
22
23Badge(value: {count: number, position?: BadgePosition \| Position, maxCount?: number, style: BadgeStyle})
24
25创建数字标记组件。
26
27从API version 9开始,该接口支持在ArkTS卡片中使用。
28
29**参数:**
30
31| 参数名 | 参数类型 | 必填 | 参数描述 |
32| -------- | -------- | -------- | -------- |
33| count | number | 是 | 设置提醒消息数。<br/>**说明:** <br/>小于等于0时不显示信息标记。<br/>取值范围:[-2147483648,2147483647],非整数时会舍去小数部分取整数部分,如5.5取5。 |
34| position | [BadgePosition](#badgeposition枚举说明)\|[Position<sup>10+</sup>](ts-types.md#position8) | 否 | 设置提示点显示位置。<br/>默认值:BadgePosition.RightTop <br/>**说明:** <br/> Position作为入参,不支持设置百分比;设置为非法值时,默认(0,0)处理。|
35| maxCount | number | 否 | 最大消息数,超过最大消息时仅显示maxCount+。<br/>默认值:99<br/>取值范围:[-2147483648,2147483647],非整数时会舍去小数部分取整数部分,如5.5取5。 |
36| style | [BadgeStyle](#badgestyle对象说明) | 是 | Badge组件可设置样式,支持设置文本颜色、尺寸、圆点颜色和尺寸。 |
37
38### Badge
39
40Badge(value: {value: string, position?: BadgePosition \| Position, style: BadgeStyle})
41
42根据字符串创建标记组件。
43
44从API version 9开始,该接口支持在ArkTS卡片中使用。
45
46**参数:**
47
48| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 |
49| -------- | -------- | -------- | -------- | -------- |
50| value | string | 是 | - | 提示内容的文本字符串。 |
51| position | [BadgePosition](#badgeposition枚举说明)\|[Position<sup>10+</sup>](ts-types.md#position8) | 否 | BadgePosition.RightTop | 设置提示点显示位置。 |
52| style | [BadgeStyle](#badgestyle对象说明) | 是 | - | Badge组件可设置样式,支持设置文本颜色、尺寸、圆点颜色和尺寸。 |
53
54## BadgePosition枚举说明
55
56从API version 9开始,该接口支持在ArkTS卡片中使用。
57
58| 名称 | 描述 |
59| -------- | -------- |
60| RightTop | 圆点显示在右上角。 |
61| Right | 圆点显示在右侧纵向居中。 |
62| Left | 圆点显示在左侧纵向居中。 |
63
64## BadgeStyle对象说明
65
66从API version 9开始,该接口支持在ArkTS卡片中使用。
67
68| 名称                      | 类型                                                         | 必填 | 描述                                                         |
69| ------------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
70| color                     | [ResourceColor](ts-types.md#resourcecolor)                   | 否   | 文本颜色。<br/>默认值:Color.White                           |
71| fontSize                  | number&nbsp;\|&nbsp;string                                   | 否   | 文本大小。<br/>默认值:10<br/>单位:vp<br/>**说明:** <br/>不支持设置百分比。 |
72| badgeSize                 | number&nbsp;\|&nbsp;string                                   | 否   | Badge的大小。<br/>默认值:16<br/>单位:vp<br/>**说明:** <br/>不支持设置百分比。当设置为非法值时,按照默认值处理。 |
73| badgeColor                | [ResourceColor](ts-types.md#resourcecolor)                   | 否   | Badge的颜色。<br/>默认值:Color.Red                          |
74| fontWeight<sup>10+</sup>  | number \|[FontWeight](ts-appendix-enums.md#fontweight) \| string | 否   | 设置文本的字体粗细。<br/>默认值:FontWeight.Normal<br/>**说明:** <br/>不支持设置百分比。 |
75| borderColor<sup>10+</sup> | [ResourceColor](ts-types.md#resourcecolor)                   | 否   | 底板描边颜色。                                               |
76| borderWidth<sup>10+</sup> | [Length](ts-types.md#length)                                 | 否   | 底板描边粗细。<br/>默认值:1<br/>单位:vp<br/>**说明:** <br/>不支持设置百分比。 |
77
78## 属性
79
80支持[通用属性](ts-universal-attributes-size.md)。
81
82## 事件
83
84支持[通用事件](ts-universal-events-click.md)。
85
86## 示例
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