ts-container-tabs.md 4.7 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

T
firstt  
tianyu 已提交
21 22
| 参数名 | 参数类型 | 必填 | 参数描述 |
| -------- | -------- | -------- | -------- |
23 24
| barPosition | BarPosition | 否 | 设置Tabs的页签位置。<br/>默认值:BarPosition.Start |
| index | number | 否 | 设置初始页签索引。<br/>默认值:0 |
T
firstt  
tianyu 已提交
25 26 27 28 29 30 31 32
| controller | [TabsController](#tabscontroller) | 否 | 设置Tabs控制器。 |

## BarPosition枚举说明

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


## 属性
Z
zengyawen 已提交
36

T
firstt  
tianyu 已提交
37
不支持[触摸热区设置](ts-universal-attributes-touch-target.md),除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性:
Z
zengyawen 已提交
38

T
firstt  
tianyu 已提交
39 40 41 42 43
| 名称 | 参数类型 | 描述 |
| -------- | -------- | -------- |
| vertical          | boolean | 设置为false是为横向Tabs,设置为true时为纵向Tabs。<br/>默认值:false |
| scrollable        | boolean | 设置为true时可以通过滑动页面进行页面切换,为false时不可滑动切换页面。<br/>默认值:true |
| barMode           | BarMode | TabBar布局模式,具体描述见BarMode枚举说明。<br/>默认值:BarMode.Fixed |
L
fix doc  
luoying_ace_admin 已提交
44 45
| barWidth          | number&nbsp;\|&nbsp;Length<sup>8+</sup>  | TabBar的宽度值。     |
| barHeight         | number&nbsp;\|&nbsp;Length<sup>8+</sup>  | TabBar的高度值。     |
T
firstt  
tianyu 已提交
46
| animationDuration | number | TabContent滑动动画时长。<br/>默认值:200 |
Z
zengyawen 已提交
47

T
firstt  
tianyu 已提交
48
## BarMode枚举说明
Z
zengyawen 已提交
49

T
firstt  
tianyu 已提交
50 51
| 名称 | 描述 |
| -------- | -------- |
52 53
| Scrollable | 每一个TabBar均使用实际布局宽度,超过总长度(横向Tabs的barWidth,纵向Tabs的barHeight)后可滑动。 |
| Fixed | 所有TabBar平均分配barWidth宽度(纵向时平均分配barHeight高度)。 |
Z
zengyawen 已提交
54 55 56

## 事件

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

T
tianyu 已提交
59
| 名称 | 功能描述 |
60
| -------- | -------- |
L
fix doc  
luoying_ace_admin 已提交
61
| onChange(event:&nbsp;(index:&nbsp;number)&nbsp;=&gt;&nbsp;void) | Tab页签切换后触发的事件。 |
Z
zengyawen 已提交
62

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
## TabsController

Tabs组件的控制器,用于控制Tabs组件进行页签切换。

### 导入对象

```
controller: TabsController = new TabsController()

```

### changeIndex

changeIndex(value: number): void

控制Tabs切换到指定页签。

T
firstt  
tianyu 已提交
80 81 82 83 84
**参数:**

| 参数名 | 参数类型 | 必填 | 参数描述 |
| -------- | -------- | -------- | -------- |
| value | number | 是 | 页签在Tabs里的索引值,索引值从0开始。 |
85

Z
zengyawen 已提交
86 87

## 示例
Z
zengyawen 已提交
88

H
geshi  
HelloCrease 已提交
89 90
```ts
// xxx.ets
Z
zengyawen 已提交
91 92 93
@Entry
@Component
struct TabsExample {
L
luoying_ace_admin 已提交
94 95 96
  @State fontColor: string = '#182431'
  @State selectedFontColor: string = '#007DFF'
  @State currentIndex: number = 0
Z
zengyawen 已提交
97 98
  private controller: TabsController = new TabsController()

L
luoying_ace_admin 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  @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 已提交
114 115
  build() {
    Column() {
Z
zengyawen 已提交
116
      Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
Z
zengyawen 已提交
117
        TabContent() {
L
luoying_ace_admin 已提交
118 119
          Column().width('100%').height('100%').backgroundColor('#00CB87')
        }.tabBar(this.TabBuilder(0, 'green'))
Z
zengyawen 已提交
120 121

        TabContent() {
L
luoying_ace_admin 已提交
122 123
          Column().width('100%').height('100%').backgroundColor('#007DFF')
        }.tabBar(this.TabBuilder(1, 'blue'))
Z
zengyawen 已提交
124 125

        TabContent() {
L
luoying_ace_admin 已提交
126 127
          Column().width('100%').height('100%').backgroundColor('#FFBF00')
        }.tabBar(this.TabBuilder(2, 'yellow'))
Z
zengyawen 已提交
128 129

        TabContent() {
L
luoying_ace_admin 已提交
130 131
          Column().width('100%').height('100%').backgroundColor('#E67C92')
        }.tabBar(this.TabBuilder(3, 'pink'))
Z
zengyawen 已提交
132
      }
L
luoying_ace_admin 已提交
133
      .vertical(false)
134
      .barMode(BarMode.Fixed)
L
luoying_ace_admin 已提交
135 136
      .barWidth(360)
      .barHeight(56)
137
      .animationDuration(400)
Z
zengyawen 已提交
138
      .onChange((index: number) => {
L
luoying_ace_admin 已提交
139
        this.currentIndex = index
Z
zengyawen 已提交
140
      })
L
luoying_ace_admin 已提交
141 142 143 144 145
      .width(360)
      .height(296)
      .margin({ top: 52 })
      .backgroundColor('#F1F3F5')
    }.width('100%')
Z
zengyawen 已提交
146 147 148 149
  }
}
```