W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
僅在Tabs中使用,對應一個切換頁簽的內(nèi)容視圖。
該組件從API Version 7開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標單獨標記該內(nèi)容的起始版本。
除支持通用屬性外,還支持以下屬性:
名稱 | 參數(shù)類型 | 描述 |
---|---|---|
tabBar | string | Resource | { icon?: string | Resource, text?: string |Resource } | CustomBuilder8+ | 設(shè)置TabBar上顯示內(nèi)容。 CustomBuilder: 構(gòu)造器,內(nèi)部可以傳入組件(API8版本以上適用)。 說明: 如果icon采用svg格式圖源,則要求svg圖源刪除其自有寬高屬性值。如采用帶有自有寬高屬性的svg圖源,icon大小則是svg本身內(nèi)置的寬高屬性值大小。 |
tabBar9+ | 設(shè)置TabBar上顯示內(nèi)容。 SubTabBarStyle: 子頁簽樣式,參數(shù)為文字。 BottomTabBarStyle: 底部頁簽和側(cè)邊頁簽樣式,參數(shù)為文字和圖片。 說明: 底部樣式?jīng)]有下劃線效果。icon異常時顯示灰色圖塊。 |
constructor(content: string | Resource)
SubTabBarStyle的構(gòu)造函數(shù)。
參數(shù):
參數(shù)名 | 參數(shù)類型 | 必填 | 參數(shù)描述 |
---|---|---|---|
content | string | Resource | 是 | 頁簽內(nèi)的文字內(nèi)容。 |
constructor(icon: string | Resource, text: string | Resource)
BottomTabBarStyle的構(gòu)造函數(shù)。
參數(shù):
示例1:
- // 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${index + 1}`)
- .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)
- .fontSize(10)
- .fontWeight(500)
- .lineHeight(14)
- }.width('100%')
- }
- build() {
- Column() {
- Tabs({ barPosition: BarPosition.End, controller: this.controller }) {
- TabContent() {
- 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%')
- }.tabBar(this.TabBuilder(0))
- TabContent() {
- 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%')
- }.tabBar(this.TabBuilder(1))
- TabContent() {
- 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))
- TabContent() {
- 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%')
- }.tabBar(this.TabBuilder(3))
- }
- .vertical(false)
- .barHeight(56)
- .onChange((index: number) => {
- this.currentIndex = index
- })
- .width(360)
- .height(190)
- .backgroundColor('#F1F3F5')
- .margin({ top: 38 })
- }.width('100%')
- }
- }
示例2:
- // 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)
- .onChange((index: number) => {
- this.currentIndex = index
- })
- .width(96)
- .height(414)
- .backgroundColor('#F1F3F5')
- .margin({ top: 52 })
- }.width('100%')
- }
- }
示例3:
- // xxx.ets
- @Entry
- @Component
- struct TabBarStyleExample {
- build() {
- Column({ space: 5 }) {
- Text("子頁簽樣式")
- 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("底部頁簽樣式")
- 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("側(cè)邊頁簽樣式")
- 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)
- }
- }
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: