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

3
通过页签进行内容视图切换的容器组件,每个页签对应一个内容视图。
Z
zengyawen 已提交
4

T
firstt  
tianyu 已提交
5 6 7
>  **说明:**
>
>  该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9 10

## 子组件
Z
zengyawen 已提交
11

12
仅可包含子组件[TabContent](ts-container-tabcontent.md)
Z
zengyawen 已提交
13

Z
zengyawen 已提交
14

T
firstt  
tianyu 已提交
15
## 接口
Z
zengyawen 已提交
16

L
fix doc  
luoying_ace_admin 已提交
17
Tabs(value?: {barPosition?: BarPosition, index?: number, controller?: [TabsController](#tabscontroller)})
Z
zengyawen 已提交
18

T
firstt  
tianyu 已提交
19
**参数:**
Z
zengyawen 已提交
20

H
HelloCrease 已提交
21 22 23 24 25
| 参数名         | 参数类型                              | 必填   | 参数描述                                     |
| ----------- | --------------------------------- | ---- | ---------------------------------------- |
| barPosition | BarPosition                       | 否    | 设置Tabs的页签位置。<br/>默认值:BarPosition.Start   |
| index       | number                            | 否    | 设置初始页签索引。<br/>默认值:0<br/>**说明:** <br/>设置为小于0的值时按默认值显示。<br/>可选值为[0, TabContent子节点数量-1]。<br/>设置不同值时,默认生效切换动效,可以设置animationDuration为0关闭动画。 |
| controller  | [TabsController](#tabscontroller) | 否    | 设置Tabs控制器。                               |
T
firstt  
tianyu 已提交
26 27 28

## BarPosition枚举说明

H
HelloCrease 已提交
29 30
| 名称    | 描述                                       |
| ----- | ---------------------------------------- |
T
firstt  
tianyu 已提交
31
| Start | vertical属性方法设置为true时,页签位于容器左侧;vertical属性方法设置为false时,页签位于容器顶部。 |
H
HelloCrease 已提交
32
| End   | vertical属性方法设置为true时,页签位于容器右侧;vertical属性方法设置为false时,页签位于容器底部。 |
Z
zengyawen 已提交
33 34 35


## 属性
Z
zengyawen 已提交
36

Y
yamila 已提交
37
除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性:
Z
zengyawen 已提交
38

H
HelloCrease 已提交
39 40 41 42 43 44 45 46 47
| 名称                       | 参数类型                                     | 描述                                       |
| ------------------------ | ---------------------------------------- | ---------------------------------------- |
| vertical                 | boolean                                  | 设置为false是为横向Tabs,设置为true时为纵向Tabs。<br/>默认值:false |
| scrollable               | boolean                                  | 设置为true时可以通过滑动页面进行页面切换,为false时不可滑动切换页面。<br/>默认值:true |
| barMode                  | BarMode                                  | TabBar布局模式,具体描述见BarMode枚举说明。<br/>默认值:BarMode.Fixed |
| barWidth                 | number&nbsp;\|&nbsp;Length<sup>8+</sup>  | TabBar的宽度值。<br/>**说明:** <br/>设置为小于0或大于Tabs宽度值时,按默认值显示。 |
| barHeight                | number&nbsp;\|&nbsp;Length<sup>8+</sup>  | TabBar的高度值。<br/>**说明:** <br/>设置为小于0或大于Tabs宽度值时,按默认值显示。 |
| animationDuration        | number                                   | TabContent滑动动画时长。不设置时,点击切换页签无动画,滑动切换有动画;设置时,点击切换和滑动切换都有动画。<br/>默认值:300 <br/>**说明:** <br/>设置为小于0或百分比时,按默认值显示。 |
| divider<sup>10+</sup>    | [DividerStyle](#dividerstyle10对象说明) \| null | 用于设置区分TabBar和TabContent的分割线样式设置分割线样式,默认不显示分割线。<br/> DividerStyle: 分割线的样式;<br/> null: 不显示分割线。 |
S
tabs  
sunjiakun 已提交
48
| fadingEdge<sup>10+</sup> | boolean                                  | 设置页签超过容器宽度时是否渐隐消失<br />默认值:true          |
C
tabs  
chensi10 已提交
49 50 51

## DividerStyle<sup>10+</sup>对象说明

H
HelloCrease 已提交
52 53 54 55 56 57
| 名称          | 参数类型                                     | 必填   | 描述                                  |
| ----------- | ---------------------------------------- | ---- | ----------------------------------- |
| strokeWidth | [Length](ts-types.md#length)             | 是    | 分割线的线宽。                             |
| color       | [ResourceColor](ts-types.md#resourcecolor) | 否    | 分割线的颜色。<br/>默认值:#33182431           |
| startMargin | [Length](ts-types.md#length)             | 否    | 分割线与侧边栏顶端的距离。<br/>默认值:0.0<br/>单位:vp |
| endMargin   | [Length](ts-types.md#length)             | 否    | 分割线与侧边栏底端的距离。<br/>默认值:0.0<br/>单位:vp |
Z
zengyawen 已提交
58

T
firstt  
tianyu 已提交
59
## BarMode枚举说明
Z
zengyawen 已提交
60

H
HelloCrease 已提交
61 62
| 名称         | 描述                                       |
| ---------- | ---------------------------------------- |
63
| Scrollable | 每一个TabBar均使用实际布局宽度,超过总长度(横向Tabs的barWidth,纵向Tabs的barHeight)后可滑动。 |
H
HelloCrease 已提交
64
| Fixed      | 所有TabBar平均分配barWidth宽度(纵向时平均分配barHeight高度)。 |
Z
zengyawen 已提交
65 66 67

## 事件

T
firstt  
tianyu 已提交
68 69
除支持[通用事件](ts-universal-events-click.md)外,还支持以下事件:

H
HelloCrease 已提交
70 71 72
| 名称                                       | 功能描述                                     |
| ---------------------------------------- | ---------------------------------------- |
| onChange(event:&nbsp;(index:&nbsp;number)&nbsp;=&gt;&nbsp;void) | Tab页签切换后触发的事件。<br>-&nbsp;index:当前显示的index索引,索引从0开始计算。<br/>触发该事件的条件:<br/>1、TabContent支持滑动时,组件触发滑动时触发。<br/>2、通过[控制器](#tabscontroller)API接口调用。<br/>3、通过[状态变量](../../quick-start/arkts-state.md)构造的属性值进行修改。<br/>4、通过页签处点击触发。 |
Z
zengyawen 已提交
73

74 75
## TabsController

Z
zhouchaobo 已提交
76
Tabs组件的控制器,用于控制Tabs组件进行页签切换。不支持一个TabsController控制多个Tabs组件。
77 78 79

### 导入对象

Y
yamila 已提交
80
```ts
81 82 83 84 85 86 87 88 89
controller: TabsController = new TabsController()
```

### changeIndex

changeIndex(value: number): void

控制Tabs切换到指定页签。

T
firstt  
tianyu 已提交
90 91
**参数:**

H
HelloCrease 已提交
92 93 94
| 参数名   | 参数类型   | 必填   | 参数描述                                     |
| ----- | ------ | ---- | ---------------------------------------- |
| value | number | 是    | 页签在Tabs里的索引值,索引值从0开始。<br/>**说明:** <br/>设置小于0或大于最大数量的值时,该事件失效。 |
95

Z
zengyawen 已提交
96 97

## 示例
Z
zengyawen 已提交
98

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

L
luoying_ace_admin 已提交
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() {
L
luoying_ace_admin 已提交
128 129
          Column().width('100%').height('100%').backgroundColor('#00CB87')
        }.tabBar(this.TabBuilder(0, 'green'))
Z
zengyawen 已提交
130 131

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

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

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

Y
yamila 已提交
160
![tabs2](figures/tabs2.gif)