• 49.HarmonyOS鸿蒙系统 App(ArkUI)Tab导航组件的使用


    HarmonyOS鸿蒙系统 App(ArkUI)Tab导航组件的使用

    图片显示

    Row()
    {
      Image($r('app.media.leaf')).height(100).width(100)
      Image($r('app.media.icon')).height(100).width(100)
    }

     

    左侧导航 

     

    1. import prompt from '@ohos.prompt';
    2. import promptAction from '@ohos.promptAction';
    3. @Entry
    4. @Component
    5. struct Index {
    6. @State message: string = 'Hello World'
    7. @State handlePopup: boolean = false
    8. build() {
    9. // Tabs() { //默认顶部导航
    10. // Tabs({ barPosition: BarPosition.End }) { //设置底部导航
    11. Tabs({ barPosition: BarPosition.Start }) { //设置顶部部导航
    12. TabContent() {
    13. Column()
    14. {
    15. Row()
    16. {
    17. Image($r('app.media.leaf')).height(100).width(100)
    18. Image($r('app.media.icon')).height(100).width(100)
    19. }
    20. Row(){
    21. Text('工号:')
    22. TextInput({text:'tintel10699487'}).backgroundColor(Color.Yellow)
    23. }
    24. Row(){
    25. Button('确定')
    26. }
    27. Row(){
    28. Text('首页的内容').fontSize(30)
    29. Button('查看')
    30. }
    31. Row(){
    32. Text('首页的内容2').fontSize(30)
    33. Button('查看2')
    34. }
    35. }
    36. }
    37. .tabBar('首页').backgroundColor(Color.Green)
    38. TabContent() {
    39. Column()
    40. {
    41. Row()
    42. {
    43. Text('这里是推荐的内容').fontSize(30)
    44. }
    45. Row()
    46. {
    47. Text('这里是推荐的内容2').fontSize(30)
    48. }
    49. Row()
    50. {
    51. Text('这里是推荐的内容3').fontSize(30)
    52. }
    53. Row()
    54. {
    55. Text('这里是推荐的内容4').fontSize(30)
    56. }
    57. Row()
    58. {
    59. Text('这里是推荐的内容5').fontSize(30)
    60. }
    61. }
    62. }
    63. .tabBar('推荐')
    64. .backgroundColor(Color.Yellow)
    65. TabContent() {
    66. Text('发现的内容').fontSize(30)
    67. }
    68. .tabBar('发现')
    69. TabContent() {
    70. Column(){
    71. Row()
    72. {
    73. Text('姓名:').fontSize(30).fontColor(Color.White)
    74. TextInput({text:"书书航"}).fontSize(30).fontColor(Color.Green)
    75. }
    76. Row()
    77. {
    78. Text('职业:').fontSize(30).fontColor(Color.White)
    79. TextInput({text:"工程师"}).fontSize(30).fontColor(Color.Green)
    80. }
    81. Row()
    82. {
    83. Text('爱好:').fontSize(30).fontColor(Color.White)
    84. TextInput({text:"音乐"}).fontSize(30).fontColor(Color.Green)
    85. }
    86. Row()
    87. {
    88. Text('户籍:').fontSize(30).fontColor(Color.White)
    89. TextInput({text:"CHN"}).fontSize(30).fontColor(Color.Green)
    90. }
    91. Row()
    92. {
    93. Text('地区:').fontSize(30).fontColor(Color.White)
    94. TextInput({text:"SHH"}).fontSize(30).fontColor(Color.Green)
    95. }
    96. Row()
    97. {
    98. Button('编辑').fontSize(30).fontColor(Color.White)
    99. Button('确认').fontSize(30).fontColor(Color.White)
    100. }
    101. }
    102. }
    103. .tabBar("我的")
    104. .backgroundColor(Color.Blue)
    105. }.vertical(true)
    106. }
    107. }

     

    Tabs组件的页面的组成包含了两个部分,分别是TabContent和TabBar。TabContent是内容页,TabBar是导航页签栏,页面结构如下图所示,根据不同的导航类型,布局会有区别,可以分为底部导航、顶部的导航、侧边的导航,其导航栏分别位于底部、顶部和侧边。

    图1 Tabs组件布局示意图

    说明

    • TabContent组件不支持设置通用宽度属性,其宽度默认撑满Tabs父组件。
    • TabContent组件不支持设置通用高度属性,其高度由Tabs父组件高度与TabBar组件高度决定。

    Tabs使用花括号包裹TabContent,如图2,其中TabContent显示相应的内容页。

    图2 Tabs与TabContent使用

    每一个TabContent对应的内容需要有一个页签,可以通过TabContent的tabBar属性进行配置。在如下TabContent组件上设置属性tabBar,可以设置其对应页签中的内容,tabBar作为内容的页签。

     
    
    1. TabContent() {
    2. Text('首页的内容').fontSize(30)
    3. }
    4. .tabBar('首页')

    设置多个内容时,需在Tabs内按照顺序放置。

     
    
    1. Tabs() {
    2. TabContent() {
    3. Text('首页的内容').fontSize(30)
    4. }
    5. .tabBar('首页')
    6. TabContent() {
    7. Text('推荐的内容').fontSize(30)
    8. }
    9. .tabBar('推荐')
    10. TabContent() {
    11. Text('发现的内容').fontSize(30)
    12. }
    13. .tabBar('发现')
    14. TabContent() {
    15. Text('我的内容').fontSize(30)
    16. }
    17. .tabBar("我的")
    18. }

     

    顶部导航:

    设置:Tabs({ barPosition: BarPosition.Start }) { //设置顶部部导航
    设置:}.vertical(false)

    1. import prompt from '@ohos.prompt';
    2. import promptAction from '@ohos.promptAction';
    3. @Entry
    4. @Component
    5. struct Index {
    6. @State message: string = 'Hello World'
    7. @State handlePopup: boolean = false
    8. build() {
    9. // Tabs() { //默认顶部导航
    10. // Tabs({ barPosition: BarPosition.End }) { //设置底部导航
    11. Tabs({ barPosition: BarPosition.Start }) { //设置顶部部导航
    12. TabContent() {
    13. Column()
    14. {
    15. Row()
    16. {
    17. Image($r('app.media.leaf')).height(100).width(100)
    18. Image($r('app.media.icon')).height(100).width(100)
    19. }
    20. Row(){
    21. Text('工号:')
    22. TextInput({text:'tintel10699487'}).backgroundColor(Color.Yellow)
    23. }
    24. Row(){
    25. Button('确定')
    26. }
    27. Row(){
    28. Text('首页的内容').fontSize(30)
    29. Button('查看')
    30. }
    31. Row(){
    32. Text('首页的内容2').fontSize(30)
    33. Button('查看2')
    34. }
    35. }
    36. }
    37. .tabBar('首页').backgroundColor(Color.Green)
    38. TabContent() {
    39. Column()
    40. {
    41. Row()
    42. {
    43. Text('这里是推荐的内容').fontSize(30)
    44. }
    45. Row()
    46. {
    47. Text('这里是推荐的内容2').fontSize(30)
    48. }
    49. Row()
    50. {
    51. Text('这里是推荐的内容3').fontSize(30)
    52. }
    53. Row()
    54. {
    55. Text('这里是推荐的内容4').fontSize(30)
    56. }
    57. Row()
    58. {
    59. Text('这里是推荐的内容5').fontSize(30)
    60. }
    61. }
    62. }
    63. .tabBar('推荐')
    64. .backgroundColor(Color.Yellow)
    65. TabContent() {
    66. Text('发现的内容').fontSize(30)
    67. }
    68. .tabBar('发现')
    69. TabContent() {
    70. Column(){
    71. Row()
    72. {
    73. Text('姓名:').fontSize(30).fontColor(Color.White)
    74. TextInput({text:"书书航"}).fontSize(30).fontColor(Color.Green)
    75. }
    76. Row()
    77. {
    78. Text('职业:').fontSize(30).fontColor(Color.White)
    79. TextInput({text:"工程师"}).fontSize(30).fontColor(Color.Green)
    80. }
    81. Row()
    82. {
    83. Text('爱好:').fontSize(30).fontColor(Color.White)
    84. TextInput({text:"音乐"}).fontSize(30).fontColor(Color.Green)
    85. }
    86. Row()
    87. {
    88. Text('户籍:').fontSize(30).fontColor(Color.White)
    89. TextInput({text:"CHN"}).fontSize(30).fontColor(Color.Green)
    90. }
    91. Row()
    92. {
    93. Text('地区:').fontSize(30).fontColor(Color.White)
    94. TextInput({text:"SHH"}).fontSize(30).fontColor(Color.Green)
    95. }
    96. Row()
    97. {
    98. Button('编辑').fontSize(30).fontColor(Color.White)
    99. Button('确认').fontSize(30).fontColor(Color.White)
    100. }
    101. }
    102. }
    103. .tabBar("我的")
    104. .backgroundColor(Color.Blue)
    105. }.vertical(false)
    106. }
    107. }

     

     

    底部导航:

    设置:Tabs({ barPosition: BarPosition.End}) { //设置底部导航
    设置:}.vertical(false)

     

     

    1. import prompt from '@ohos.prompt';
    2. import promptAction from '@ohos.promptAction';
    3. @Entry
    4. @Component
    5. struct Index {
    6. @State message: string = 'Hello World'
    7. @State handlePopup: boolean = false
    8. build() {
    9. // Tabs() { //默认顶部导航
    10. Tabs({ barPosition: BarPosition.End }) { //设置底部导航
    11. //Tabs({ barPosition: BarPosition.Start }) { //设置顶部部导航
    12. TabContent() {
    13. Column()
    14. {
    15. Row()
    16. {
    17. Image($r('app.media.leaf')).height(100).width(100)
    18. Image($r('app.media.icon')).height(100).width(100)
    19. }
    20. Row(){
    21. Text('工号:')
    22. TextInput({text:'tintel10699487'}).backgroundColor(Color.Yellow)
    23. }
    24. Row(){
    25. Button('确定')
    26. }
    27. Row(){
    28. Text('首页的内容').fontSize(30)
    29. Button('查看')
    30. }
    31. Row(){
    32. Text('首页的内容2').fontSize(30)
    33. Button('查看2')
    34. }
    35. }
    36. }
    37. .tabBar('首页').backgroundColor(Color.Green)
    38. TabContent() {
    39. Column()
    40. {
    41. Row()
    42. {
    43. Text('这里是推荐的内容').fontSize(30)
    44. }
    45. Row()
    46. {
    47. Text('这里是推荐的内容2').fontSize(30)
    48. }
    49. Row()
    50. {
    51. Text('这里是推荐的内容3').fontSize(30)
    52. }
    53. Row()
    54. {
    55. Text('这里是推荐的内容4').fontSize(30)
    56. }
    57. Row()
    58. {
    59. Text('这里是推荐的内容5').fontSize(30)
    60. }
    61. }
    62. }
    63. .tabBar('推荐')
    64. .backgroundColor(Color.Yellow)
    65. TabContent() {
    66. Text('发现的内容').fontSize(30)
    67. }
    68. .tabBar('发现')
    69. TabContent() {
    70. Column(){
    71. Row()
    72. {
    73. Text('姓名:').fontSize(30).fontColor(Color.White)
    74. TextInput({text:"书书航"}).fontSize(30).fontColor(Color.Green)
    75. }
    76. Row()
    77. {
    78. Text('职业:').fontSize(30).fontColor(Color.White)
    79. TextInput({text:"工程师"}).fontSize(30).fontColor(Color.Green)
    80. }
    81. Row()
    82. {
    83. Text('爱好:').fontSize(30).fontColor(Color.White)
    84. TextInput({text:"音乐"}).fontSize(30).fontColor(Color.Green)
    85. }
    86. Row()
    87. {
    88. Text('户籍:').fontSize(30).fontColor(Color.White)
    89. TextInput({text:"CHN"}).fontSize(30).fontColor(Color.Green)
    90. }
    91. Row()
    92. {
    93. Text('地区:').fontSize(30).fontColor(Color.White)
    94. TextInput({text:"SHH"}).fontSize(30).fontColor(Color.Green)
    95. }
    96. Row()
    97. {
    98. Button('编辑').fontSize(30).fontColor(Color.White)
    99. Button('确认').fontSize(30).fontColor(Color.White)
    100. }
    101. }
    102. }
    103. .tabBar("我的")
    104. .backgroundColor(Color.Blue)
    105. }.vertical(false)
    106. }
    107. }

     

  • 相关阅读:
    Yolo v8 目标识别 单目测距 速度检测
    Kubecost - Kubernetes 开支监控和管理
    gitlab没有push权限,重设账号密码仍然不行,问题解决
    探索React Router:实现动态二级路由
    PAT 甲级 A1072 Gas Station
    基于 ceph-deploy 部署 Ceph 集群 超详细
    Python:b站多个视频爬取下载
    Git从入门到起飞(详细)
    【前端优化 & Vue项目优化】 如何避免浏览器卡顿,实现性能优化cdn?
    NodeJS 5分钟 连接 Redis 读写操作
  • 原文地址:https://blog.csdn.net/txwtech/article/details/137891451