ts-container-tabs.md 8.8 KB
Newer Older
Z
zengyawen 已提交
1 2
# Tabs

E
ester.zhou 已提交
3
The **\<Tabs>** component is a container component that allows users to switch between content views through tabs. Each tab page corresponds to a content view.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5
>  **NOTE**
E
ester.zhou 已提交
6
>
E
ester.zhou 已提交
7
>  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
esterzhou 已提交
10
## Child Components
Z
zengyawen 已提交
11

E
ester.zhou 已提交
12
Only the [\<TabContent>](ts-container-tabcontent.md) child component is supported.
Z
zengyawen 已提交
13 14 15 16


## APIs

E
ester.zhou 已提交
17
Tabs(value?: {barPosition?: BarPosition, index?: number, controller?: [TabsController](#tabscontroller)})
Z
zengyawen 已提交
18

E
ester.zhou 已提交
19
**Parameters**
Z
zengyawen 已提交
20

E
ester.zhou 已提交
21 22 23 24 25
| Name        | Type                             | Mandatory  | Description                                    |
| ----------- | --------------------------------- | ---- | ---------------------------------------- |
| barPosition | BarPosition                       | No   | Position of the **\<Tabs>** component.<br>Default value: **BarPosition.Start**  |
| index       | number                            | No   | Initial tab index.<br>Default value: **0**<br>**NOTE**<br><br>A value less than 0 evaluates to the default value.<br>The value ranges from 0 to the number of **\<TabContent>** subnodes minus 1.<br>When this parameter is set to different values, the slide animation for tab switching is enabled by default. To disable the animation, set **animationDuration** to **0**.|
| controller  | [TabsController](#tabscontroller) | No   | Tab controller.                              |
E
ester.zhou 已提交
26 27 28

## BarPosition

E
ester.zhou 已提交
29 30
| Name   | Description                                      |
| ----- | ---------------------------------------- |
E
ester.zhou 已提交
31
| Start | If the **vertical** attribute is set to **true**, the tab is on the left of the container. If the **vertical** attribute is set to **false**, the tab is on the top of the container.|
E
ester.zhou 已提交
32
| End   | If the **vertical** attribute is set to **true**, the tab is on the right of the container. If the **vertical** attribute is set to **false**, the tab is at the bottom of the container.|
Z
zengyawen 已提交
33 34 35


## Attributes
Z
zengyawen 已提交
36

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

E
ester.zhou 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| Name                      | Type                                    | Description                                      |
| ------------------------ | ---------------------------------------- | ---------------------------------------- |
| vertical                 | boolean                                  | Whether to use vertical tabs. The value **true** means to use vertical tabs, and **false** means to use horizontal tabs.<br>Default value: **false**|
| scrollable               | boolean                                  | Whether the tabs are scrollable. The value **true** means that the tabs are scrollable, and **false** means the opposite.<br>Default value: **true**|
| barMode                  | BarMode                                  | Tab bar layout mode. For details, see **BarMode**.<br>Default value: **BarMode.Fixed**|
| barWidth                 | number \| Length<sup>8+</sup>  | Width of the tab bar.<br>**NOTE**<br><br>A value less than 0 or greater than the width of the **\<Tabs>** component evaluates to the default value.|
| barHeight                | number \| Length<sup>8+</sup>  | Height of the tab bar.<br>**NOTE**<br><br>A value less than 0 or greater than the width of the **\<Tabs>** component evaluates to the default value.|
| animationDuration        | number                                   | Duration of the slide animation for tab switching. If this parameter is set, the tab switching animation is played when the user switches between tabs by sliding or clicking. If this parameter is not set, the tab switching animation is played only when the user switches between tabs by sliding.<br>Default value: **300**<br>**NOTE**<br>A value less than 0 or in percentage evaluates to the default value. |
| divider<sup>10+</sup>    | [DividerStyle](#dividerstyle10) \| null | Whether the divider is displayed for the **\<TabBar>** and **\<TabContent>** components and the divider style. By default, the divider is not displayed.<br> **DividerStyle**: divider style.<br> **null**: The divider is not displayed.|
| FadingEdge<sup>10+</sup> | boolean                                  | Whether the tab fades out when it exceeds the container width.<br>Default value: **true**         |

## DividerStyle<sup>10+</sup>

| Name         | Type                                    | Mandatory  | Description                                 |
| ----------- | ---------------------------------------- | ---- | ----------------------------------- |
| strokeWidth | [Length](ts-types.md#length)             | Yes   | Width of the divider.                            |
| color       | [ResourceColor](ts-types.md#resourcecolor) | No   | Color of the divider.<br>Default value: **#33182431**          |
| startMargin | [Length](ts-types.md#length)             | No   | Distance between the divider and the top of the sidebar.<br>Default value: **0.0**<br>Unit: vp|
| endMargin   | [Length](ts-types.md#length)             | No   | Distance between the divider and the bottom of the sidebar.<br>Default value: **0.0**<br>Unit: vp|
Z
zengyawen 已提交
58

E
ester.zhou 已提交
59
## BarMode
Z
zengyawen 已提交
60

E
ester.zhou 已提交
61 62
| Name        | Description                                      |
| ---------- | ---------------------------------------- |
E
ester.zhou 已提交
63
| Scrollable | The width of each tab is determined by the actual layout. The tabs are scrollable in the following case: In horizontal layout, the total width exceeds the tab bar width; in horizontal layout, the total height exceeds the tab bar height.|
E
ester.zhou 已提交
64
| Fixed      | The width of each tab is determined by equally dividing the number of tabs by the bar width (or bar height in the vertical layout).|
Z
zengyawen 已提交
65 66 67

## Events

E
ester.zhou 已提交
68
In addition to the [universal events](ts-universal-events-click.md), the following events are supported.
E
ester.zhou 已提交
69

E
ester.zhou 已提交
70 71 72
| Name                                        | Description                                                  |
| ------------------------------------------- | ------------------------------------------------------------ |
| onChange(event: (index: number) =&gt; void) | Triggered when a tab is switched.<br>- **index**: index of the active tab. The index starts from 0.<br>This event is triggered when any of the following conditions is met:<br>1. The **\<TabContent>** component supports sliding, and the user slides on the tab bar.<br>2. The [Controller](#tabscontroller) API is called.<br>3. The attribute value is updated using a [state variable](../../quick-start/arkts-state.md).<br>4. A tab is clicked. |
Z
zengyawen 已提交
73

E
ester.zhou 已提交
74 75
## TabsController

E
ester.zhou 已提交
76
Defines a tab controller, which is used to control switching of tabs. One **TabsController** cannot control multiple **\<Tabs>** components.
E
ester.zhou 已提交
77

E
ester.zhou 已提交
78 79
### Objects to Import

E
ester.zhou 已提交
80
```ts
E
ester.zhou 已提交
81 82 83 84 85 86 87 88 89
controller: TabsController = new TabsController()
```

### changeIndex

changeIndex(value: number): void

Switches to the specified tab.

E
ester.zhou 已提交
90
**Parameters**
E
ester.zhou 已提交
91

E
ester.zhou 已提交
92 93 94
| Name  | Type  | Mandatory  | Description                                    |
| ----- | ------ | ---- | ---------------------------------------- |
| value | number | Yes   | Index of the tab. The value starts from 0.<br>**NOTE**<br><br>If this parameter is set to a value less than 0 or greater than the maximum number, the event will be invalid.|
Z
zengyawen 已提交
95 96


E
ester.zhou 已提交
97
## Example
Z
zengyawen 已提交
98

E
ester.zhou 已提交
99 100
```ts
// xxx.ets
Z
zengyawen 已提交
101 102 103
@Entry
@Component
struct TabsExample {
E
ester.zhou 已提交
104 105 106
  @State fontColor: string = '#182431'
  @State selectedFontColor: string = '#007DFF'
  @State currentIndex: number = 0
Z
zengyawen 已提交
107 108
  private controller: TabsController = new TabsController()

E
ester.zhou 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  @Builder TabBuilder(index: number, name: string) {
    Column() {
      Text(name)
        .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)
        .fontSize(16)
        .fontWeight(this.currentIndex === index ? 500 : 400)
        .lineHeight(22)
        .margin({ top: 17, bottom: 7 })
      Divider()
        .strokeWidth(2)
        .color('#007DFF')
        .opacity(this.currentIndex === index ? 1 : 0)
    }.width('100%')
  }

Z
zengyawen 已提交
124 125
  build() {
    Column() {
Z
zengyawen 已提交
126
      Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
Z
zengyawen 已提交
127
        TabContent() {
E
ester.zhou 已提交
128 129
          Column().width('100%').height('100%').backgroundColor('#00CB87')
        }.tabBar(this.TabBuilder(0, 'green'))
Z
zengyawen 已提交
130 131

        TabContent() {
E
ester.zhou 已提交
132 133
          Column().width('100%').height('100%').backgroundColor('#007DFF')
        }.tabBar(this.TabBuilder(1, 'blue'))
Z
zengyawen 已提交
134 135

        TabContent() {
E
ester.zhou 已提交
136 137
          Column().width('100%').height('100%').backgroundColor('#FFBF00')
        }.tabBar(this.TabBuilder(2, 'yellow'))
Z
zengyawen 已提交
138 139

        TabContent() {
E
ester.zhou 已提交
140 141
          Column().width('100%').height('100%').backgroundColor('#E67C92')
        }.tabBar(this.TabBuilder(3, 'pink'))
Z
zengyawen 已提交
142
      }
E
ester.zhou 已提交
143 144 145 146 147
      .vertical(false)
      .barMode(BarMode.Fixed)
      .barWidth(360)
      .barHeight(56)
      .animationDuration(400)
Z
zengyawen 已提交
148
      .onChange((index: number) => {
E
ester.zhou 已提交
149
        this.currentIndex = index
Z
zengyawen 已提交
150
      })
E
ester.zhou 已提交
151 152 153 154 155
      .width(360)
      .height(296)
      .margin({ top: 52 })
      .backgroundColor('#F1F3F5')
    }.width('100%')
Z
zengyawen 已提交
156 157 158 159
  }
}
```

E
ester.zhou 已提交
160
![tabs2](figures/tabs2.gif)
E
ester.zhou 已提交
161
<!--no_check-->