微信小程序-自定义顶部组件customHeader

1、开启配置自定义顶部

在这里插入图片描述

{
  "window": {
    "navigationStyle":"custom"
  }
}

在app.json的文件window配置"navigationStyle": "custom"属性即可

2、自定义顶部计算原理

2.1 获取系统状态栏的高度和屏幕宽度

使用wx.getSystemInfo这个函数获取系统状态栏的高度和屏幕宽度。
在这里插入图片描述

2.2 获取右上角胶囊位置信息

使用wx.getMenuButtonBoundingClientRect()方法获取右上角胶囊位置信息。
关键问题在于自定义胶囊的上、下和左边距。
在这里插入图片描述

2.3 自定义顶部距离计算代码

app.js代码如下

App({
    // 小程序初始化
    onLaunch: function() {
        // 获取自定义顶部高度相关参数
        let capsuleObj = wx.getMenuButtonBoundingClientRect();
        // console.log("==胶囊信息==");
        // console.log(capsuleObj);

        wx.getSystemInfo({
            success: (res) => {
                // console.log("==获取系统信息==");
                // console.log(res)
                var statusBarHeight = res.statusBarHeight; //顶部状态栏高度

                this.globalData.capsuleObj = capsuleObj;
                this.globalData.titleHeight = statusBarHeight + capsuleObj.height + (capsuleObj.top - statusBarHeight) * 2;
            },
            failure() {
            }
        })
    },

    // 全局缓存
    globalData: {
        loginStatus: false,
    },
})

3、封装自定义顶部

3.1效果图展示

在这里插入图片描述
在这里插入图片描述

3.2组件代码

index.wxml

<!--components/customHeader/index.wxml-->
<view class="customHeader_box" style="height:{{titleHeight}}px; background-color:{{bgColor}};">
    <!-- 菜单 -->
    <view wx:if="{{menuFlag}}" class="menu_box" style="height:{{capsuleObj.height}}px; top:{{capsuleObj.top}}px;">
        <view class="customIcon" bindtap="meunClick">
            <image src="/images/customMenu.png"></image>
        </view>
    </view>

    <!-- 返回+首页 -->
    <view wx:if="{{backHomeFlag}}" class="backHome_box" style="width:{{capsuleObj.width}}px; height:{{capsuleObj.height}}px; top:{{capsuleObj.top}}px;">
        <view class="customIcon backIcon" bindtap="backClick">
            <image src="/images/customBack.png"></image>
        </view>
        <view class="customIcon homeIcon" bindtap="homeClick">
            <image src="/images/customHome.png"></image>
        </view>
    </view>

    <!-- 标题 -->
    <view class="customHeader_title" style="top:{{capsuleObj.top}}px; height:{{capsuleObj.height}}px; line-height:{{capsuleObj.height}}px;">
        {{customTitle}}
    </view>
</view>

<!-- 自定义顶部距离修正 -->
<view class="customWrap" style="height:{{titleHeight}}px;"></view>

index.wxss

/* components/customHeader/index.wxss */

.customHeader_box {
    width: 100%;
    overflow: hidden;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 99999;
}

.customIcon {
    flex: 1;
}

.customIcon image {
    width: 30rpx;
    height: 30rpx;
}

/* 菜单 */
.menu_box{
    text-align: center;
    box-sizing: border-box;
    overflow: hidden;
    position: absolute;
    left: 10px;
    z-index: 11;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.menu_box .customIcon image{
    width: 36rpx;
    height: 36rpx;
}

/* 返回+首页 */

.backHome_box {
    text-align: center;
    border: 1px solid #e5e5e5;
    border-radius: 20px;
    box-sizing: border-box;
    overflow: hidden;
    position: absolute;
    left: 10px;
    z-index: 11;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.backIcon {
    border-right: 1rpx solid #e5e5e5;
}

/* 标题 */

.customHeader_title {
    width: 100%;
    padding-left: 115px;
    padding-right: 115px;
    text-align: center;
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
    text-overflow: ellipsis;
    box-sizing: border-box;
    overflow: hidden;
    white-space: nowrap;
    position: absolute;
    left: 0;
    z-index: 10;
}

/* 自定义顶部距离修正 */
.customWrap{
    width: 100%;
    position: relative;
    left: 0;
    z-index: 99998;
}

index.js

// components/customHeader/index.js
const app = getApp();

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        customTitle: String,
        bgColor: {
            type: String,
            value: '#fff'
        },
        menuFlag: {
            type: Boolean,
            value: false
        },
        backHomeFlag: {
            type: Boolean,
            value: false
        },
    },

    /**
     * 组件的初始数据
     */
    data: {

    },

    attached: function() {
        this.setData({
            titleHeight: app.globalData.titleHeight,
            capsuleObj: app.globalData.capsuleObj
        })
    },

    options: {
        multipleSlots: true, //开启多slot
    },

    /**
     * 组件的方法列表
     */
    methods: {
        // 菜单
        meunClick: function () {
            wx.navigateTo({
                url: '/pages/menu/menu',
            })
        },

        // 返回
        backClick: function() {
            wx.navigateBack({
                delta: 1
            })
        },
        
        // 回首页
        homeClick: function() {
            wx.navigateTo({
                url: '/pages/index/index'
            })
        },
    }
})

index.json

{
    "component": true
}

4、组件使用方法

index.wxml

<!--pages/index/index.wxml-->

<!-- 自定义顶部 -->
<customHeader menuFlag customTitle="首页"></customHeader>

<view class="url">
	<navigator hover-class="none" url="../child/child">跳到子页</navigator>
</view>

ps:我这边封装了2个样式,menuFlag是菜单的。backHomeFlag是“返回+首页”样式的。

json配置

{
    "usingComponents": {
        "customHeader": "/components/customHeader/index"
    }
}

5、小结

此组件兼容性,可以兼容安卓、IOS、刘海屏。

链接在此:
微信小程序自定义顶部组件

另附:
微信小程序自定义tabbar组件

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您好,关于EasyExcel检查表头是否匹配的问题,可以通过以下步骤实现: 1. 首先定义一个类来存储Excel表头的信息,例如: ``` public class ExcelHeader { private String title; // 表头名称 private int columnIndex; // 列索引 // 省略getter和setter方法 } ``` 2. 在读取Excel文件时,可以将Excel表头存储到一个List<ExcelHeader>中,例如: ``` List<ExcelHeader> headerList = new ArrayList<>(); for (int i = 0; i < cellList.size(); i++) { String cellValue = cellList.get(i); ExcelHeader header = new ExcelHeader(); header.setTitle(cellValue); header.setColumnIndex(i); headerList.add(header); } ``` 3. 接着,在定义Excel表头的时候,也可以定义一个与ExcelHeader类相同的类来存储表头信息,例如: ``` public class CustomHeader { private String title; // 表头名称 private String field; // 对应的数据栏字段 // 省略getter和setter方法 } ``` 4. 生成Excel文件时,可以将自定义表头与数据栏对应的处理方式存储到一个List<CustomHeader>中,例如: ``` List<CustomHeader> customHeaderList = new ArrayList<>(); CustomHeader customHeader1 = new CustomHeader(); customHeader1.setTitle("姓名"); customHeader1.setField("name"); customHeaderList.add(customHeader1); CustomHeader customHeader2 = new CustomHeader(); customHeader2.setTitle("年龄"); customHeader2.setField("age"); customHeaderList.add(customHeader2); ``` 5. 最后,在检查表头是否匹配时,可以遍历Excel表头和自定义表头的List,比较它们的title是否一致,例如: ``` boolean isHeaderMatch = true; for (int i = 0; i < headerList.size(); i++) { ExcelHeader excelHeader = headerList.get(i); CustomHeader customHeader = customHeaderList.get(i); if (!excelHeader.getTitle().equals(customHeader.getTitle())) { isHeaderMatch = false; break; } } ``` 以上就是利用EasyExcel生成Excel文件时自定义表头与数据栏对应的处理方式,并检查表头是否匹配的方法。希望对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

 康 

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值