切換頁簽的內(nèi)容視圖

2024-01-22 17:59 更新

僅在Tabs中使用,對應一個切換頁簽的內(nèi)容視圖。

說明

該組件從API Version 7開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標單獨標記該內(nèi)容的起始版本。

子組件

支持單個子組件。

說明

可內(nèi)置系統(tǒng)組件和自定義組件,支持渲染控制類型(if/else、ForEachLazyForEach)。

接口

TabContent()

屬性

除支持通用屬性外,還支持以下屬性:

名稱

參數(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+

SubTabBarStyle | BottomTabBarStyle

設(shè)置TabBar上顯示內(nèi)容。

SubTabBarStyle: 子頁簽樣式,參數(shù)為文字。

BottomTabBarStyle: 底部頁簽和側(cè)邊頁簽樣式,參數(shù)為文字和圖片。

說明:

底部樣式?jīng)]有下劃線效果。icon異常時顯示灰色圖塊。

說明
  • TabContent組件不支持設(shè)置通用寬度屬性,其寬度默認撐滿Tabs父組件。
  • TabContent組件不支持設(shè)置通用高度屬性,其高度由Tabs父組件高度與TabBar組件高度決定。
  • vertical屬性為false值,交換上述2個限制。
  • TabContent組件不支持內(nèi)容過長時頁面的滑動,如需頁面滑動,可嵌套List使用。

SubTabBarStyle9+

子頁簽樣式。

constructor9+

constructor(content: string | Resource)

SubTabBarStyle的構(gòu)造函數(shù)。

參數(shù):

參數(shù)名

參數(shù)類型

必填

參數(shù)描述

content

string | Resource

頁簽內(nèi)的文字內(nèi)容。

BottomTabBarStyle9+

底部頁簽和側(cè)邊頁簽樣式。

constructor9+

constructor(icon: string | Resource, text: string | Resource)

BottomTabBarStyle的構(gòu)造函數(shù)。

參數(shù):

參數(shù)名

參數(shù)類型

必填

參數(shù)描述

icon

string | Resource

頁簽內(nèi)的圖片內(nèi)容。

text

string | Resource

頁簽內(nèi)的文字內(nèi)容。

示例

示例1:

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct TabContentExample {
  5. @State fontColor: string = '#182431'
  6. @State selectedFontColor: string = '#007DFF'
  7. @State currentIndex: number = 0
  8. private controller: TabsController = new TabsController()
  9. @Builder TabBuilder(index: number) {
  10. Column() {
  11. Image(this.currentIndex === index ? '/common/public_icon_on.svg' : '/common/public_icon_off.svg')
  12. .width(24)
  13. .height(24)
  14. .margin({ bottom: 4 })
  15. .objectFit(ImageFit.Contain)
  16. Text(`Tab${index + 1}`)
  17. .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)
  18. .fontSize(10)
  19. .fontWeight(500)
  20. .lineHeight(14)
  21. }.width('100%')
  22. }
  23. build() {
  24. Column() {
  25. Tabs({ barPosition: BarPosition.End, controller: this.controller }) {
  26. TabContent() {
  27. Column() {
  28. Text('Tab1')
  29. .fontSize(36)
  30. .fontColor('#182431')
  31. .fontWeight(500)
  32. .opacity(0.4)
  33. .margin({ top: 30, bottom: 56.5 })
  34. Divider()
  35. .strokeWidth(0.5)
  36. .color('#182431')
  37. .opacity(0.05)
  38. }.width('100%')
  39. }.tabBar(this.TabBuilder(0))
  40. TabContent() {
  41. Column() {
  42. Text('Tab2')
  43. .fontSize(36)
  44. .fontColor('#182431')
  45. .fontWeight(500)
  46. .opacity(0.4)
  47. .margin({ top: 30, bottom: 56.5 })
  48. Divider()
  49. .strokeWidth(0.5)
  50. .color('#182431')
  51. .opacity(0.05)
  52. }.width('100%')
  53. }.tabBar(this.TabBuilder(1))
  54. TabContent() {
  55. Column() {
  56. Text('Tab3')
  57. .fontSize(36)
  58. .fontColor('#182431')
  59. .fontWeight(500)
  60. .opacity(0.4)
  61. .margin({ top: 30, bottom: 56.5 })
  62. Divider()
  63. .strokeWidth(0.5)
  64. .color('#182431')
  65. .opacity(0.05)
  66. }.width('100%')
  67. }.tabBar(this.TabBuilder(2))
  68. TabContent() {
  69. Column() {
  70. Text('Tab4')
  71. .fontSize(36)
  72. .fontColor('#182431')
  73. .fontWeight(500)
  74. .opacity(0.4)
  75. .margin({ top: 30, bottom: 56.5 })
  76. Divider()
  77. .strokeWidth(0.5)
  78. .color('#182431')
  79. .opacity(0.05)
  80. }.width('100%')
  81. }.tabBar(this.TabBuilder(3))
  82. }
  83. .vertical(false)
  84. .barHeight(56)
  85. .onChange((index: number) => {
  86. this.currentIndex = index
  87. })
  88. .width(360)
  89. .height(190)
  90. .backgroundColor('#F1F3F5')
  91. .margin({ top: 38 })
  92. }.width('100%')
  93. }
  94. }

示例2:

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct TabContentExample {
  5. @State fontColor: string = '#182431'
  6. @State selectedFontColor: string = '#007DFF'
  7. @State currentIndex: number = 0
  8. private controller: TabsController = new TabsController()
  9. @Builder TabBuilder(index: number) {
  10. Column() {
  11. Image(this.currentIndex === index ? '/common/public_icon_on.svg' : '/common/public_icon_off.svg')
  12. .width(24)
  13. .height(24)
  14. .margin({ bottom: 4 })
  15. .objectFit(ImageFit.Contain)
  16. Text('Tab')
  17. .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)
  18. .fontSize(10)
  19. .fontWeight(500)
  20. .lineHeight(14)
  21. }.width('100%').height('100%').justifyContent(FlexAlign.Center)
  22. }
  23. build() {
  24. Column() {
  25. Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
  26. TabContent()
  27. .tabBar(this.TabBuilder(0))
  28. TabContent()
  29. .tabBar(this.TabBuilder(1))
  30. TabContent()
  31. .tabBar(this.TabBuilder(2))
  32. TabContent()
  33. .tabBar(this.TabBuilder(3))
  34. }
  35. .vertical(true)
  36. .barWidth(96)
  37. .barHeight(414)
  38. .onChange((index: number) => {
  39. this.currentIndex = index
  40. })
  41. .width(96)
  42. .height(414)
  43. .backgroundColor('#F1F3F5')
  44. .margin({ top: 52 })
  45. }.width('100%')
  46. }
  47. }

示例3:

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct TabBarStyleExample {
  5. build() {
  6. Column({ space: 5 }) {
  7. Text("子頁簽樣式")
  8. Column() {
  9. Tabs({ barPosition: BarPosition.Start }) {
  10. TabContent() {
  11. Column().width('100%').height('100%').backgroundColor(Color.Pink)
  12. }.tabBar(new SubTabBarStyle('Pink'))
  13. TabContent() {
  14. Column().width('100%').height('100%').backgroundColor(Color.Yellow)
  15. }.tabBar(new SubTabBarStyle('Yellow'))
  16. TabContent() {
  17. Column().width('100%').height('100%').backgroundColor(Color.Blue)
  18. }.tabBar(new SubTabBarStyle('Blue'))
  19. TabContent() {
  20. Column().width('100%').height('100%').backgroundColor(Color.Green)
  21. }.tabBar(new SubTabBarStyle('Green'))
  22. }
  23. .vertical(false)
  24. .scrollable(true)
  25. .barMode(BarMode.Fixed)
  26. .onChange((index: number) => {
  27. console.info(index.toString())
  28. })
  29. .width('100%')
  30. .backgroundColor(0xF1F3F5)
  31. }.width('100%').height(200)
  32. Text("底部頁簽樣式")
  33. Column() {
  34. Tabs({ barPosition: BarPosition.End }) {
  35. TabContent() {
  36. Column().width('100%').height('100%').backgroundColor(Color.Pink)
  37. }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'pink'))
  38. TabContent() {
  39. Column().width('100%').height('100%').backgroundColor(Color.Yellow)
  40. }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Yellow'))
  41. TabContent() {
  42. Column().width('100%').height('100%').backgroundColor(Color.Blue)
  43. }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Blue'))
  44. TabContent() {
  45. Column().width('100%').height('100%').backgroundColor(Color.Green)
  46. }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Green'))
  47. }
  48. .vertical(false)
  49. .scrollable(true)
  50. .barMode(BarMode.Fixed)
  51. .onChange((index: number) => {
  52. console.info(index.toString())
  53. })
  54. .width('100%')
  55. .backgroundColor(0xF1F3F5)
  56. }.width('100%').height(200)
  57. Text("側(cè)邊頁簽樣式")
  58. Column() {
  59. Tabs({ barPosition: BarPosition.Start }) {
  60. TabContent() {
  61. Column().width('100%').height('100%').backgroundColor(Color.Pink)
  62. }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'pink'))
  63. TabContent() {
  64. Column().width('100%').height('100%').backgroundColor(Color.Yellow)
  65. }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Yellow'))
  66. TabContent() {
  67. Column().width('100%').height('100%').backgroundColor(Color.Blue)
  68. }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Blue'))
  69. TabContent() {
  70. Column().width('100%').height('100%').backgroundColor(Color.Green)
  71. }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Green'))
  72. }
  73. .vertical(true).scrollable(true).barMode(BarMode.Fixed)
  74. .onChange((index: number) => {
  75. console.info(index.toString())
  76. })
  77. .width('100%')
  78. .backgroundColor(0xF1F3F5)
  79. }.width('100%').height(400)
  80. }
  81. }
  82. }

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號