[小程序开发]使用 wx.getSystemInfo实现自适应————(2020.3.15学习笔记)

在开始之前,先介绍一下wx.getSystemInfo(Object object),通过该API可以获取当前设备的高度,宽度(官方文档链接https://developers.weixin.qq.com/miniprogram/dev/api/base/system/system-info/wx.getSystemInfo.html)。
然后在知道可以通过wx.getSystemInfo(Object object)可获取当前设备高度的基础上,如何实现自适应的方法自然也出来了,那就是,将获取的设备高度应用在页面上控件的高度,相对距离,这样页面控件会随着设备高度的不同而发生变化。
那么,以上思路按顺序实现起来,第一步就是通过wx.getSystemInfo(Object object)可获取当前设备高度,这个实现的效果如下

图中Console输出中的windowHeight: 568,windowWidth: 320,就是当前设备的高宽。
实现的代码如下

// pages/Weather/Weather.js

const app=getApp();
Page({

  /**
   * 页面的初始数据
   */

  data: {
  
    Page_Height:0,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


    wx.getSystemInfo({
      success:function(res){
        console.log(res);
        app.globalData.Page_Height_Current=res.windowHeight;
      }
    })
    // 再通过setData更改Page()里面的data,动态更新页面的数据
   this.setData({
 
    Page_Height:parseInt(app.globalData.Page_Height_Current)
   })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

第二步就是,将获取的高度应用在页面控件,应用之后,自适应效果就实现了,然后再稍微装饰一下,实现出来的最终效果如下图
在这里插入图片描述
实现的代码如下
wxml的代码

<!--pages/Weather/Weather.wxml-->
<view class="Weather_AllPage" style=" margin-top:{{Forehead_Height+40}}rpx ;">
<view class="Weather_Title" >
   <text class="Time_Title">{{Time_Year+","+Time_Mouth+" "+Time_Day}}</text>
   <text class="Page_Name_Title">天气界面</text>
   
</view>
<view class="Weather_Body">
<view class="Weather_HumidityAndTemperature_Items">

  <view class="Weather_Humidity_Item" style="height: {{Page_Height*3/10}}px;">
     <image src="/My_Image/My_Weather_Icon/水滴.png" style="height: {{Page_Height*1/27}}px;"></image>
     <text class="Weather_Humidity_Item_Title">湿度</text>
      <text class="Weather_Humidity_Item_Number"   style="  margin-top: {{Page_Height*1/11}}px;">32</text>
       <text class="Weather_Humidity_Item_Time">{{Time_Hours+"时"+Time_Minutes+"分"+Time_Seconds+"秒"}}</text>
  </view>
   
  <view class="Weather_Temperature_Item" style="height: {{Page_Height*2/5}}px;">
   <image src="/My_Image/My_Weather_Icon/温度.png" style="height: {{Page_Height*1/27}}px;"></image>
     <text class="Weather_Temperature_Item_Title">温度</text>
      <text class="Weather_Temperature_Item_Number" style="  margin-top: {{Page_Height*19/100}}px;">7°</text>
       <text class="Weather_Temperature_Item_Time">{{Time_Hours+"时"+Time_Minutes+"分"+Time_Seconds+"秒"}}</text>
  </view>
  </view>
  <view class="Weather_AqiAndPower_Items"> 

    <view class="Weather_Aqi_Item" style="height: {{Page_Height*38/100}}px;">
      <image src="/My_Image/My_Weather_Icon/空气质量.png" style="height: {{Page_Height*1/27}}px;"></image>
     <text class="Weather_Aqi_Item_Title">空气质量</text>
      <text class="Weather_Aqi_Item_Number"  style="  margin-top: {{Page_Height*10/60}}px;">35</text>
       <text class="Weather_Aqi_Item_Time">{{Time_Hours+"时"+Time_Minutes+"分"+Time_Seconds+"秒"}}</text>
  </view>
  <view class="Weather_Power_Item" style="height: {{Page_Height*32/100}}px;">
       <image src="/My_Image/My_Weather_Icon/风.png" style="height: {{Page_Height*1/27}}px;"></image>
     <text class="Weather_Power_Item_Title" >风力</text>
      <text class="Weather_Power_Item_Number" style="  margin-top: {{Page_Height*18/165}}px;">2级</text>
       <text class="Weather_Power_Item_Time">{{Time_Hours+"时"+Time_Minutes+"分"+Time_Seconds+"秒"}}</text>
  </view>
  </view>
 
</view>
</view>

<My_Tabbar activeIdx="0"/>

wxss的代码

/* pages/Weather/Weather.wxss */
.Weather_AllPage{
  display: flex;
  flex-direction: column;
  margin-left: 5%;
  
}
.Time_Title{
  font-size: 27rpx;
  color: #dbdbdb;
}
.Page_Name_Title{
  margin-top: 12rpx;
  font-size: 40rpx;
  font-family: Microsoft YaHei;
  font-weight: 600;
}
.Weather_Title{
  display: flex;
  flex-direction: column;
   
}
.Weather_Body{
  display: flex;
  margin-top: 40rpx;

}
.Weather_HumidityAndTemperature_Items{
  width: 45%;
  display: flex;
  flex-direction: column;
}
.Weather_Humidity_Item{
  
  border-radius: 20rpx;
  display: flex;
  flex-direction: column;
  background-color: #6A2AFE;
}
.Weather_Humidity_Item image{
  width: 50rpx;
  
  margin-top: 10%;
  margin-left: 8%;
}
.Weather_Humidity_Item_Title{
  color: white;
  font-family: YouYuan;
  font-weight: 700;
  margin-top: 6%;
  margin-left: 8%;
}
.Weather_Humidity_Item_Number{

  margin-left: 25rpx;
  font-size: 58rpx;
  color: white;
}
.Weather_Humidity_Item_Time{
  color: white;
  margin-left: 25rpx;
  font-size: 18rpx;
}
.Weather_Temperature_Item{
  margin-top: 10%;
  height: 550rpx;
  display: flex;
  flex-direction: column;

  border-radius: 20rpx;
  background-color: #FE6760;
}
.Weather_Temperature_Item image{
  width: 50rpx;
  height: 50rpx;
  margin-top: 10%;
  margin-left: 8%;
}
.Weather_Temperature_Item_Title{
  color: white;
  font-family: YouYuan;
  font-weight: 700;
  margin-top: 6%;
  margin-left: 8%;
}
.Weather_Temperature_Item_Number{

  margin-left: 25rpx;
  font-size: 58rpx;
  color: white;
}
.Weather_Temperature_Item_Time{
  color: white;
  margin-left: 25rpx;
  font-size: 18rpx;
}
.Weather_AqiAndPower_Items{
  width: 45%;
  margin-left: 5%;
  display: flex;
  flex-direction: column;
}

.Weather_Aqi_Item
{

  display: flex;
  flex-direction: column;

  border-radius: 20rpx;
  background-color: #69C3F6;
}
.Weather_Aqi_Item image{
  width: 50rpx;

  margin-top: 10%;
  margin-left: 8%;
}
.Weather_Aqi_Item_Title{
  color: white;
  font-family: YouYuan;
  font-weight: 700;
  margin-top: 6%;
  margin-left: 8%;
}
.Weather_Aqi_Item_Number{

  margin-left: 25rpx;
  font-size: 58rpx;
  color: white;
}
.Weather_Aqi_Item_Time{
  color: white;
  margin-left: 25rpx;
  font-size: 18rpx;
}
.Weather_Power_Item{
  margin-top: 10%;

  border-radius: 20rpx;
  display: flex;
  flex-direction: column;
  background-color: #F2AE83;
}
.Weather_Power_Item image{
  width: 50rpx;
  height: 50rpx;
  margin-top: 10%;
  margin-left: 8%;
}
.Weather_Power_Item_Title{
  color: white;
  font-family: YouYuan;
  font-weight: 700;
  margin-top: 6%;
  margin-left: 8%;
}
.Weather_Power_Item_Number{

  margin-left: 25rpx;
  font-size: 58rpx;
  color: white;
}
.Weather_Power_Item_Time{
  color: white;
  margin-left: 25rpx;
  font-size: 18rpx;
}
wx.getSystemInfo是一个用于获取系统信息的方法。它有两种调用方式:wx.getSystemInfowx.getSystemInfoAsync。其中,wx.getSystemInfo是异步的调用格式,但是是同步返回;而wx.getSystemInfoAsync是异步获取系统信息的方式。 该方法返回的结果包含了一些属性,其中包括success、fail和complete。success是接口调用成功时的回调函数,fail是接口调用失败时的回调函数,complete是接口调用结束时的回调函数,无论成功还是失败都会执行。 根据引用的内容,如果你在适配iPhone X屏幕时遇到问题,你可以使用wx.getSystemInfo来获取系统信息,从而更好地适配iPhone X的屏幕。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [微信小程序开发之——获取系统信息](https://blog.csdn.net/Calvin_zhou/article/details/120504257)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [微信小程序适配 iPhone X 总结](https://download.csdn.net/download/weixin_38624519/16209098)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值