ts-container-tabcontent.md 4.0 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 Component
Z
zengyawen 已提交
11 12 13 14

This component supports only one child component.


Z
zengyawen 已提交
15
## APIs
Z
zengyawen 已提交
16

Z
zengyawen 已提交
17 18 19 20
TabContent()


## Attributes
Z
zengyawen 已提交
21

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

E
ester.zhou 已提交
24 25
| Name| Type| Description|
| -------- | -------- | -------- |
E
ester.zhou 已提交
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.|
Z
zengyawen 已提交
27

E
ester.zhou 已提交
28 29 30 31
>  **NOTE**
> - 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.
> - The **\<TabContent>** component does not support setting of the [touch target](ts-universal-attributes-touch-target.md).
Z
zengyawen 已提交
32 33 34 35


## Example

E
ester.zhou 已提交
36 37
```ts
// xxx.ets
Z
zengyawen 已提交
38 39 40 41 42 43 44
@Entry
@Component
struct TabContentExample  {
  @State fontColor: string = 'rgba(0, 0, 0, 0.4)'
  @State selectedFontColor: string = 'rgba(10, 30, 255, 1)'
  @State currentIndex: number = 0
  private controller: TabsController = new TabsController()
Z
zengyawen 已提交
45
  @Builder TabBuilder(index: number) {
Z
zengyawen 已提交
46
    Column() {
Z
zengyawen 已提交
47 48 49 50
      Image(this.currentIndex === index ? '/resources/ic_public_contacts_filled_selected.png' : '/resources/ic_public_contacts_filled.png')
        .width(10)
        .height(10)
        .opacity(this.currentIndex === index ? 1 : 0.4)
Z
zengyawen 已提交
51
        .objectFit(ImageFit.Contain)
Z
zengyawen 已提交
52 53
      Text(`Tab${(index > 2 ? (index - 1) : index) + 1}`)
        .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)
Z
zengyawen 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
        .fontSize(10)
        .margin({top: 2})
    }
  }

  @Builder AddBuilder() {
    Column() {
      Image(this.currentIndex === 2 ? '/resources/ic_public_add_norm_filled_selected.png' : '/resources/ic_public_add_norm_filled.png')
        .width(this.currentIndex === 2 ? 26 : 24)
        .height(this.currentIndex === 2 ? 26 : 24)
        .opacity(this.currentIndex === 2 ? 1 : 0.4)
        .objectFit(ImageFit.Contain)
        .animation({duration: 200})
    }
  }

  build() {
    Column() {
Z
zengyawen 已提交
72
      Tabs({ barPosition: BarPosition.End, controller: this.controller }) {
Z
zengyawen 已提交
73
        TabContent() {
E
ester.zhou 已提交
74
          Flex({justifyContent: FlexAlign.Center}) {
Z
zengyawen 已提交
75 76
            Text('Tab1').fontSize(32)
          }
Z
zengyawen 已提交
77
        }.tabBar(this.TabBuilder(0))
Z
zengyawen 已提交
78 79

        TabContent() {
E
ester.zhou 已提交
80
          Flex({justifyContent: FlexAlign.Center}) {
Z
zengyawen 已提交
81 82
            Text('Tab2').fontSize(32)
          }
Z
zengyawen 已提交
83
        }.tabBar(this.TabBuilder(1))
Z
zengyawen 已提交
84 85

        TabContent() {
E
ester.zhou 已提交
86
          Flex({justifyContent: FlexAlign.Center}) {
Z
zengyawen 已提交
87 88
            Text('Add').fontSize(32)
          }
Z
zengyawen 已提交
89
        }.tabBar(this.AddBuilder())
Z
zengyawen 已提交
90 91

        TabContent() {
E
ester.zhou 已提交
92
          Flex({justifyContent: FlexAlign.Center}) {
Z
zengyawen 已提交
93 94
            Text('Tab3').fontSize(32)
          }
Z
zengyawen 已提交
95
        }.tabBar(this.TabBuilder(3))
Z
zengyawen 已提交
96 97

        TabContent() {
E
ester.zhou 已提交
98
          Flex({justifyContent: FlexAlign.Center}) {
Z
zengyawen 已提交
99 100
            Text('Tab4').fontSize(32)
          }
Z
zengyawen 已提交
101
        }.tabBar(this.TabBuilder(4))
Z
zengyawen 已提交
102 103 104 105 106 107 108 109 110 111 112
      }
      .vertical(false)
      .barWidth(300).barHeight(56)
      .onChange((index: number) => {
        this.currentIndex = index
      })
      .width('90%').backgroundColor('rgba(241, 243, 245, 0.95)')
    }.width('100%').height(200).margin({ top: 5 })
  }
}
```
Z
zengyawen 已提交
113

Z
zengyawen 已提交
114
![en-us_image_0000001256978331](figures/en-us_image_0000001256978331.gif)