ts-container-tabcontent.md 11.1 KB
Newer Older
Z
zengyawen 已提交
1
# TabContent
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
The **\<TabContent>** component is used only in the **\<Tabs>** component. It corresponds to the content view of a switched tab page.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5 6 7
>  **NOTE**
>
>  This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9

E
ester.zhou 已提交
10
## Child Components
Z
zengyawen 已提交
11 12 13

This component supports only one child component.

E
ester.zhou 已提交
14 15
>  **NOTE**
>
E
ester.zhou 已提交
16 17
>  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.

Z
zengyawen 已提交
18

Z
zengyawen 已提交
19
## APIs
Z
zengyawen 已提交
20

Z
zengyawen 已提交
21 22 23 24
TabContent()


## Attributes
Z
zengyawen 已提交
25

E
ester.zhou 已提交
26
In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
Z
zengyawen 已提交
27

E
ester.zhou 已提交
28 29
| Name| Type| Description|
| -------- | -------- | -------- |
E
ester.zhou 已提交
30
| tabBar | string \| [Resource](ts-types.md#resource) \| {<br>icon?: string \| [Resource](ts-types.md#resource),<br>text?: string \| [Resource](ts-types.md#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.<br>If the content set exceeds the space provided by the tab bar, it will be clipped.|
E
ester.zhou 已提交
31
| tabBar<sup>9+</sup> | [SubTabBarStyle](#subtabbarstyle9) \| [BottomTabBarStyle](#bottomtabbarstyle9) | 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.<br>**NOTE**<br>The bottom tab style does not include an underline.<br>When an icon display error occurs, a gray blank block is displayed.|
Z
zengyawen 已提交
32

E
ester.zhou 已提交
33
>  **NOTE**
E
ester.zhou 已提交
34 35 36
>
>  - 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.
>  - 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.
E
ester.zhou 已提交
37
>  - If the **vertical** attribute is **false**, the width and height descriptions are swapped in the preceding two restrictions.
E
ester.zhou 已提交
38
>  - **\<TabContent>** does not support page scrolling. If page scrolling is required, consider nesting a list.
Z
zengyawen 已提交
39

40 41 42 43 44 45 46 47
## SubTabBarStyle<sup>9+</sup>

Implements the subtab style.

### constructor<sup>9+</sup>

constructor(content: string | Resource)

E
ester.zhou 已提交
48
Constructor used to create a **SubTabBarStyle** instance.
49 50 51 52 53

**Parameters**

| Name| Type        | Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
54 55
| content | string \| [Resource](ts-types.md#resource) | Yes| Text for the tab. Since API version 10, the type of **content** is **ResourceStr**.|

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

## BottomTabBarStyle<sup>9+</sup>

Implements the bottom and side tab style.

### constructor<sup>9+</sup>

constructor(icon: string | Resource, text: string | Resource)

A constructor used to create a **BottomTabBarStyle** instance.

**Parameters**

| Name| Type        | Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
71 72 73
| icon | string \| [Resource](ts-types.md#resource) | Yes| Image for the tab. Since API version 10, the type of **icon** is **ResourceStr**.|
| text | string \| [Resource](ts-types.md#resource) | Yes| Text for the tab. Since API version 10, the type of **text** is **ResourceStr**.|

Z
zengyawen 已提交
74 75 76

## Example

77 78
Example 1:

E
ester.zhou 已提交
79 80
```ts
// xxx.ets
Z
zengyawen 已提交
81 82
@Entry
@Component
83 84 85
struct TabContentExample {
  @State fontColor: string = '#182431'
  @State selectedFontColor: string = '#007DFF'
Z
zengyawen 已提交
86 87
  @State currentIndex: number = 0
  private controller: TabsController = new TabsController()
88

Z
zengyawen 已提交
89
  @Builder TabBuilder(index: number) {
Z
zengyawen 已提交
90
    Column() {
91 92 93 94
      Image(this.currentIndex === index ? '/common/public_icon_on.svg' : '/common/public_icon_off.svg')
        .width(24)
        .height(24)
        .margin({ bottom: 4 })
Z
zengyawen 已提交
95
        .objectFit(ImageFit.Contain)
96
      Text(`Tab${index + 1}`)
Z
zengyawen 已提交
97
        .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)
Z
zengyawen 已提交
98
        .fontSize(10)
99 100 101
        .fontWeight(500)
        .lineHeight(14)
    }.width('100%')
Z
zengyawen 已提交
102 103 104 105
  }

  build() {
    Column() {
Z
zengyawen 已提交
106
      Tabs({ barPosition: BarPosition.End, controller: this.controller }) {
Z
zengyawen 已提交
107
        TabContent() {
108 109 110 111 112 113 114 115 116 117 118 119
          Column() {
            Text('Tab1')
              .fontSize(36)
              .fontColor('#182431')
              .fontWeight(500)
              .opacity(0.4)
              .margin({ top: 30, bottom: 56.5 })
            Divider()
              .strokeWidth(0.5)
              .color('#182431')
              .opacity(0.05)
          }.width('100%')
Z
zengyawen 已提交
120
        }.tabBar(this.TabBuilder(0))
Z
zengyawen 已提交
121 122

        TabContent() {
123 124 125 126 127 128 129 130 131 132 133 134
          Column() {
            Text('Tab2')
              .fontSize(36)
              .fontColor('#182431')
              .fontWeight(500)
              .opacity(0.4)
              .margin({ top: 30, bottom: 56.5 })
            Divider()
              .strokeWidth(0.5)
              .color('#182431')
              .opacity(0.05)
          }.width('100%')
Z
zengyawen 已提交
135
        }.tabBar(this.TabBuilder(1))
Z
zengyawen 已提交
136 137

        TabContent() {
138 139 140 141 142 143 144 145 146 147 148 149 150
          Column() {
            Text('Tab3')
              .fontSize(36)
              .fontColor('#182431')
              .fontWeight(500)
              .opacity(0.4)
              .margin({ top: 30, bottom: 56.5 })
            Divider()
              .strokeWidth(0.5)
              .color('#182431')
              .opacity(0.05)
          }.width('100%')
        }.tabBar(this.TabBuilder(2))
Z
zengyawen 已提交
151 152

        TabContent() {
153 154 155 156 157 158 159 160 161 162 163 164
          Column() {
            Text('Tab4')
              .fontSize(36)
              .fontColor('#182431')
              .fontWeight(500)
              .opacity(0.4)
              .margin({ top: 30, bottom: 56.5 })
            Divider()
              .strokeWidth(0.5)
              .color('#182431')
              .opacity(0.05)
          }.width('100%')
Z
zengyawen 已提交
165
        }.tabBar(this.TabBuilder(3))
Z
zengyawen 已提交
166 167
      }
      .vertical(false)
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
      .barHeight(56)
      .onChange((index: number) => {
        this.currentIndex = index
      })
      .width(360)
      .height(190)
      .backgroundColor('#F1F3F5')
      .margin({ top: 38 })
    }.width('100%')
  }
}
```

![tabContent](figures/tabContent1.gif)

Example 2:

```ts
// xxx.ets
@Entry
@Component
struct TabContentExample {
  @State fontColor: string = '#182431'
  @State selectedFontColor: string = '#007DFF'
  @State currentIndex: number = 0
  private controller: TabsController = new TabsController()

  @Builder TabBuilder(index: number) {
    Column() {
      Image(this.currentIndex === index ? '/common/public_icon_on.svg' : '/common/public_icon_off.svg')
        .width(24)
        .height(24)
        .margin({ bottom: 4 })
        .objectFit(ImageFit.Contain)
      Text('Tab')
        .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)
        .fontSize(10)
        .fontWeight(500)
        .lineHeight(14)
    }.width('100%').height('100%').justifyContent(FlexAlign.Center)
  }

  build() {
    Column() {
      Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
        TabContent()
          .tabBar(this.TabBuilder(0))
        TabContent()
          .tabBar(this.TabBuilder(1))
        TabContent()
          .tabBar(this.TabBuilder(2))
        TabContent()
          .tabBar(this.TabBuilder(3))
      }
      .vertical(true)
      .barWidth(96)
      .barHeight(414)
Z
zengyawen 已提交
225 226 227
      .onChange((index: number) => {
        this.currentIndex = index
      })
228 229 230 231 232
      .width(96)
      .height(414)
      .backgroundColor('#F1F3F5')
      .margin({ top: 52 })
    }.width('100%')
Z
zengyawen 已提交
233 234 235
  }
}
```
Z
zengyawen 已提交
236

237
![tabContent](figures/tabContent2.gif)
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

Example 3:

```ts
// xxx.ets
@Entry
@Component
struct TabBarStyleExample {
  build() {
    Column({ space: 5 }) {
      Text ("Subtab Style")
      Column() {
        Tabs({ barPosition: BarPosition.Start }) {
          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Pink)
          }.tabBar(new SubTabBarStyle('Pink'))

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Yellow)
          }.tabBar(new SubTabBarStyle('Yellow'))

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Blue)
          }.tabBar(new SubTabBarStyle('Blue'))

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Green)
          }.tabBar(new SubTabBarStyle('Green'))
        }
        .vertical(false)
        .scrollable(true)
        .barMode(BarMode.Fixed)
        .onChange((index: number) => {
          console.info(index.toString())
        })
        .width('100%')
        .backgroundColor(0xF1F3F5)
      }.width('100%').height(200)
      Text ("Bottom Tab Style")
      Column() {
        Tabs({ barPosition: BarPosition.End }) {
          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Pink)
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'pink'))

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Yellow)
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Yellow'))

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Blue)
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Blue'))

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Green)
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Green'))
        }
        .vertical(false)
        .scrollable(true)
        .barMode(BarMode.Fixed)
        .onChange((index: number) => {
          console.info(index.toString())
        })
        .width('100%')
        .backgroundColor(0xF1F3F5)
      }.width('100%').height(200)
      Text ("Side Tab Style")
      Column() {
        Tabs({ barPosition: BarPosition.Start }) {
          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Pink)
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'pink'))

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Yellow)
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Yellow'))

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Blue)
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Blue'))

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Green)
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Green'))
        }
        .vertical(true).scrollable(true).barMode(BarMode.Fixed)
        .onChange((index: number) => {
          console.info(index.toString())
        })
        .width('100%')
        .backgroundColor(0xF1F3F5)
      }.width('100%').height(400)
    }
  }
}
```

![tabbarStyle](figures/TabBarStyle.jpeg)