• 微信小程序中使用tabBar


    前景知识

    前面我们学习过:在app.json里对小程序进行全局配置,app.json的文件内容是一个JSON对象,包含很多属性,其中一个常用属性就是 tabBar。如果小程序是一个多tab应用,就可以通过tabBar指定tab栏的表现,以及tab切换时显示的对应页面。

    tabBar 的属性值也是一个对象,该对象包含以下常用属性:

    • color,tab上文字的默认颜色,仅支持十六进制颜色,必填。
    • selectedColor,tab上文字选中时的颜色,仅支持十六进制颜色,必填。
    • backgroundColor,tab的背景颜色,仅支持十六进制颜色,必填。
    • position,tabBar的位置,仅支持两个值:bottomtop ,默认值是bottom
    • list,tab的列表,是一个数组,数组元素最少2个,最多5个。每个数组元素都是一个对象,包含以下属性值:
      • pathPath,页面路径,必须在pages中定义,必填。
      • text,tab上的文字,必填。
      • iconPath,图片路径,非必填。icon大小限制为40kb,建议尺寸为81px*81px,不支持网络图片。当position值为top时,不显示icon。
      • selectedIconPath,选中时的图片路径,非必填。icon大小限制为40kb,建议尺寸为81px*81px,不支持网络图片。当positiontop时,不显示icon。

    小程序项目

    pages下新建Page:home(主页)、camera(拍照)、user(个人中心)。

    • pages/home/home.wxmlhome.wxsshome.jshome.json
    • pages/camera/camera.wxmlcamera.wxsscamera.jscamera.json
    • pages/user/user.wxmluser.wxssuser.jsuser.json

    项目根目录下新建文件夹:static,static下新建文件夹:images,images下新建文件夹:tabIcons,tabIcons下存放tabBar要用到的图标。

    代码涉及的主要文件有:

    1. app.json
    2. pages/home/home.wxml
    3. pages/camera/camera.wxml
    4. pages/user/user.wxml

    在这里插入图片描述

    app.json

    {
      "pages": [
        "pages/home/home",
        "pages/camera/camera",
        "pages/user/user"
      ],
      "window": {
        "navigationBarBackgroundColor": "#971a22",
        "navigationBarTitleText": "首页",
        "navigationBarTextStyle": "white"
      },
      "tabBar": {
        "color": "#000000",
        "selectedColor": "#971a22",
        "backgroundColor": "#ffffff",
        "list": [
          {
            "pagePath": "pages/home/home",
            "text": "主页",
            "iconPath": "/static/images/tabIcons/home.png",
            "selectedIconPath": "/static/images/tabIcons/home-fill.png"
          },
          {
            "pagePath": "pages/camera/camera",
            "text": "拍照",
            "iconPath": "/static/images/tabIcons/camera.png",
            "selectedIconPath": "/static/images/tabIcons/camera-fill.png"
          },
          {
            "pagePath": "pages/user/user",
            "text": "个人中心",
            "iconPath": "/static/images/tabIcons/user.png",
            "selectedIconPath": "/static/images/tabIcons/user-fill.png"
          }
        ]
      },
      "style": "v2",
      "sitemapLocation": "sitemap.json"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    pages/home/home.wxml

    <view class="camera">
      Here is Home Page
    </view>
    
    • 1
    • 2
    • 3

    pages/camera/camera.wxml

    <view class="camera">
      Here is Camera Page
    </view>
    
    • 1
    • 2
    • 3

    pages/user/user.wxml

    <view class="camera">
      Here is User Page
    </view>
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    相关链接

    新建微信小程序项目

  • 相关阅读:
    Pytorch 转ONNX详解
    如何使用yum 安装php7.2
    手拉手带你用 Vue3 + VantUI 写一个移动端脚手架 系列二 (页面布局与兼容)
    2019阿里java面试题(一)
    长短时记忆网络(Long Short Term Memory,LSTM)详解
    HTML CSS大学生期末网页大作业 DW个人网页设计 人物介绍 历史人物岳飞介绍
    数据结构:树和二叉树的概念及性质
    linux 系统redis常用命令
    代码检视的新姿势!在IDEA中得到沉浸式Code Review新体验
    Stable Diffusion 是否使用 GPU?
  • 原文地址:https://blog.csdn.net/qzw752890913/article/details/125553859