• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# TabContent
2
3The **\<TabContent>** component is used only in the **\<Tabs>** component. It corresponds to the content view of a switched tab page.
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
15## APIs
16
17TabContent()
18
19
20## Attributes
21
22In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
23
24| Name| Type| Description|
25| -------- | -------- | -------- |
26| tabBar | string \| Resource \| {<br>icon?: string \| Resource,<br>text?: string \| Resource<br>}<br>\| [CustomBuilder](ts-types.md)<sup>8+</sup> | Content displayed on the tab bar.<br>**CustomBuilder**: builder, to which components can be passed (applicable to API version 8 and later versions).<br>**NOTE**<br>If an icon uses an SVG image, the width and height attributes of the SVG image must be deleted. Otherwise, the icon size will be determined by the width and height attributes of the SVG image. |
27| tabBar<sup>9+</sup> | [SubTabBarStyle](#subtabbarstyle) \| [BottomTabBarStyle](#bottomtabbarstyle) | Content displayed on the tab bar.<br>**SubTabBarStyle**: subtab style. It takes text as its input parameter.<br>**BottomTabBarStyle**: bottom and side tab style. It takes text and images as its input parameters.|
28
29>  **NOTE**
30> - The **\<TabContent>** component does not support setting of the common width attribute. By default, its width is the same as that of the parent **\<Tabs>** component.
31> - The **\<TabContent>** component does not support setting of the common height attribute. Its height is determined by the height of the parent **\<Tabs>** component and the **\<TabBar>** component.
32> - **\<TabContent>** does not support page scrolling. If page scrolling is required, consider nesting a list.
33
34## SubTabBarStyle<sup>9+</sup>
35
36Implements the subtab style.
37
38### constructor<sup>9+</sup>
39
40constructor(content: string | Resource)
41
42A constructor used to create a **SubTabBarStyle** instance.
43
44**Parameters**
45
46| Name| Type        | Mandatory| Description|
47| -------- | -------- | -------- | -------- |
48| content | string \| [Resource](ts-types.md#resource) | Yes| Text for the tab.|
49
50## BottomTabBarStyle<sup>9+</sup>
51
52Implements the bottom and side tab style.
53
54### constructor<sup>9+</sup>
55
56constructor(icon: string | Resource, text: string | Resource)
57
58A constructor used to create a **BottomTabBarStyle** instance.
59
60**Parameters**
61
62| Name| Type        | Mandatory| Description|
63| -------- | -------- | -------- | -------- |
64| icon | string \| [Resource](ts-types.md#resource) | Yes| Image for the tab.|
65| text | string \| [Resource](ts-types.md#resource) | Yes| Text for the tab.|
66
67## Example
68
69Example 1:
70
71```ts
72// xxx.ets
73@Entry
74@Component
75struct TabContentExample {
76  @State fontColor: string = '#182431'
77  @State selectedFontColor: string = '#007DFF'
78  @State currentIndex: number = 0
79  private controller: TabsController = new TabsController()
80
81  @Builder TabBuilder(index: number) {
82    Column() {
83      Image(this.currentIndex === index ? '/common/public_icon_on.svg' : '/common/public_icon_off.svg')
84        .width(24)
85        .height(24)
86        .margin({ bottom: 4 })
87        .objectFit(ImageFit.Contain)
88      Text(`Tab${index + 1}`)
89        .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)
90        .fontSize(10)
91        .fontWeight(500)
92        .lineHeight(14)
93    }.width('100%')
94  }
95
96  build() {
97    Column() {
98      Tabs({ barPosition: BarPosition.End, controller: this.controller }) {
99        TabContent() {
100          Column() {
101            Text('Tab1')
102              .fontSize(36)
103              .fontColor('#182431')
104              .fontWeight(500)
105              .opacity(0.4)
106              .margin({ top: 30, bottom: 56.5 })
107            Divider()
108              .strokeWidth(0.5)
109              .color('#182431')
110              .opacity(0.05)
111          }.width('100%')
112        }.tabBar(this.TabBuilder(0))
113
114        TabContent() {
115          Column() {
116            Text('Tab2')
117              .fontSize(36)
118              .fontColor('#182431')
119              .fontWeight(500)
120              .opacity(0.4)
121              .margin({ top: 30, bottom: 56.5 })
122            Divider()
123              .strokeWidth(0.5)
124              .color('#182431')
125              .opacity(0.05)
126          }.width('100%')
127        }.tabBar(this.TabBuilder(1))
128
129        TabContent() {
130          Column() {
131            Text('Tab3')
132              .fontSize(36)
133              .fontColor('#182431')
134              .fontWeight(500)
135              .opacity(0.4)
136              .margin({ top: 30, bottom: 56.5 })
137            Divider()
138              .strokeWidth(0.5)
139              .color('#182431')
140              .opacity(0.05)
141          }.width('100%')
142        }.tabBar(this.TabBuilder(2))
143
144        TabContent() {
145          Column() {
146            Text('Tab4')
147              .fontSize(36)
148              .fontColor('#182431')
149              .fontWeight(500)
150              .opacity(0.4)
151              .margin({ top: 30, bottom: 56.5 })
152            Divider()
153              .strokeWidth(0.5)
154              .color('#182431')
155              .opacity(0.05)
156          }.width('100%')
157        }.tabBar(this.TabBuilder(3))
158      }
159      .vertical(false)
160      .barHeight(56)
161      .onChange((index: number) => {
162        this.currentIndex = index
163      })
164      .width(360)
165      .height(190)
166      .backgroundColor('#F1F3F5')
167      .margin({ top: 38 })
168    }.width('100%')
169  }
170}
171```
172
173![tabContent](figures/tabContent1.gif)
174
175Example 2:
176
177```ts
178// xxx.ets
179@Entry
180@Component
181struct TabContentExample {
182  @State fontColor: string = '#182431'
183  @State selectedFontColor: string = '#007DFF'
184  @State currentIndex: number = 0
185  private controller: TabsController = new TabsController()
186
187  @Builder TabBuilder(index: number) {
188    Column() {
189      Image(this.currentIndex === index ? '/common/public_icon_on.svg' : '/common/public_icon_off.svg')
190        .width(24)
191        .height(24)
192        .margin({ bottom: 4 })
193        .objectFit(ImageFit.Contain)
194      Text('Tab')
195        .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)
196        .fontSize(10)
197        .fontWeight(500)
198        .lineHeight(14)
199    }.width('100%').height('100%').justifyContent(FlexAlign.Center)
200  }
201
202  build() {
203    Column() {
204      Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
205        TabContent()
206          .tabBar(this.TabBuilder(0))
207        TabContent()
208          .tabBar(this.TabBuilder(1))
209        TabContent()
210          .tabBar(this.TabBuilder(2))
211        TabContent()
212          .tabBar(this.TabBuilder(3))
213      }
214      .vertical(true)
215      .barWidth(96)
216      .barHeight(414)
217      .onChange((index: number) => {
218        this.currentIndex = index
219      })
220      .width(96)
221      .height(414)
222      .backgroundColor('#F1F3F5')
223      .margin({ top: 52 })
224    }.width('100%')
225  }
226}
227```
228
229![tabContent](figures/tabContent2.gif)
230
231Example 3:
232
233```ts
234// xxx.ets
235@Entry
236@Component
237struct TabBarStyleExample {
238  build() {
239    Column({ space: 5 }) {
240      Text ("Subtab Style")
241      Column() {
242        Tabs({ barPosition: BarPosition.Start }) {
243          TabContent() {
244            Column().width('100%').height('100%').backgroundColor(Color.Pink)
245          }.tabBar(new SubTabBarStyle('Pink'))
246
247          TabContent() {
248            Column().width('100%').height('100%').backgroundColor(Color.Yellow)
249          }.tabBar(new SubTabBarStyle('Yellow'))
250
251          TabContent() {
252            Column().width('100%').height('100%').backgroundColor(Color.Blue)
253          }.tabBar(new SubTabBarStyle('Blue'))
254
255          TabContent() {
256            Column().width('100%').height('100%').backgroundColor(Color.Green)
257          }.tabBar(new SubTabBarStyle('Green'))
258        }
259        .vertical(false)
260        .scrollable(true)
261        .barMode(BarMode.Fixed)
262        .onChange((index: number) => {
263          console.info(index.toString())
264        })
265        .width('100%')
266        .backgroundColor(0xF1F3F5)
267      }.width('100%').height(200)
268      Text ("Bottom Tab Style")
269      Column() {
270        Tabs({ barPosition: BarPosition.End }) {
271          TabContent() {
272            Column().width('100%').height('100%').backgroundColor(Color.Pink)
273          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'pink'))
274
275          TabContent() {
276            Column().width('100%').height('100%').backgroundColor(Color.Yellow)
277          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Yellow'))
278
279          TabContent() {
280            Column().width('100%').height('100%').backgroundColor(Color.Blue)
281          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Blue'))
282
283          TabContent() {
284            Column().width('100%').height('100%').backgroundColor(Color.Green)
285          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Green'))
286        }
287        .vertical(false)
288        .scrollable(true)
289        .barMode(BarMode.Fixed)
290        .onChange((index: number) => {
291          console.info(index.toString())
292        })
293        .width('100%')
294        .backgroundColor(0xF1F3F5)
295      }.width('100%').height(200)
296      Text ("Side Tab Style")
297      Column() {
298        Tabs({ barPosition: BarPosition.Start }) {
299          TabContent() {
300            Column().width('100%').height('100%').backgroundColor(Color.Pink)
301          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'pink'))
302
303          TabContent() {
304            Column().width('100%').height('100%').backgroundColor(Color.Yellow)
305          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Yellow'))
306
307          TabContent() {
308            Column().width('100%').height('100%').backgroundColor(Color.Blue)
309          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Blue'))
310
311          TabContent() {
312            Column().width('100%').height('100%').backgroundColor(Color.Green)
313          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Green'))
314        }
315        .vertical(true).scrollable(true).barMode(BarMode.Fixed)
316        .onChange((index: number) => {
317          console.info(index.toString())
318        })
319        .width('100%')
320        .backgroundColor(0xF1F3F5)
321      }.width('100%').height(400)
322    }
323  }
324}
325```
326
327![tabbarStyle](figures/TabBarStyle.jpeg)
328