当前位置:   article > 正文

【快应用】响应式布局适配横竖屏或折叠屏_折叠屏宽度px

折叠屏宽度px

 【关键词】

响应式布局、折叠屏、横竖屏

【问题背景】

当前开发者在开发快应用时,往往将designWidth设置为设备屏幕的宽度,这时,应用的内容会随着设备宽度的变大而拉伸显示,导致在大屏、横屏、折叠屏展开时显示效果不好。

在折叠屏合起和展开的效果如下,可以看出页面各元素尺寸在展开时明显变大了。

cke_7130.png

【解决方案】

通过使用快应用的响应式布局能力开发新应用或者改造已有应用,可以使快应用在手机、平板、智慧屏等各种尺寸的设备都有良好的展示效果。

一、配置manifest文件

1、将minPlatformVersion配置为1066及以上版本

2、修改config节点下的designWidth属性为device-width。

二、布局页面元素

根据常见的屏幕宽度对页面元素的尺寸和排列位置进行规划。常见屏幕宽度如下:

  •  正常手机竖屏的宽度:360px
  • 正常手机横屏的宽度:640px、720px 等
  • 折叠屏展开的宽度:733px
  • 折叠屏合起来时的宽度:383px

三、动态布局

根据屏幕的宽度来判断一行渲染几张图片,以下示例实现了根据屏幕宽度来控制list展示列数的效果。

当简单的动态布局无法实现所需效果时,可以考虑使用MediaQuery进行适配,详细参见链接:

https://developer.huawei.com/consumer/cn/doc/development/quickApp-References/quickapp-mediaquery-0000001170210011

实现如下:

  1. <template>
  2. <!-- Only one root node is allowed in template. -->
  3. <div class="container">
  4. <div class="btn-container">
  5. <text onclick="switchOrientation" class="orientation-btn">{{$t('message.framework.screenSwitch.switchScreen')}}</text>
  6. </div>
  7. <list class="list">
  8. <list-item type="box" class="list-item bg-blue"></list-item>
  9. <list-item type="box" class="list-item bg-green"></list-item>
  10. <list-item type="box" class="list-item bg-gray"></list-item>
  11. <list-item type="box" class="list-item bg-red"></list-item>
  12. </list>
  13. </div>
  14. </template>
  15. <style lang="sass">
  16. .container {
  17. flex-direction: column;
  18. padding-bottom: 50px;
  19. }
  20. .bg-green {
  21. background-color: #64bb5c;
  22. }
  23. .bg-blue {
  24. background-color: #46b1e3;
  25. }
  26. .bg-gray {
  27. background-color: #5d5e5d;
  28. }
  29. .bg-red {
  30. background-color: #e64566;
  31. }
  32. .orientation-btn {
  33. color: #0a59f7;
  34. font-size: 32px;
  35. font-weight: 500;
  36. }
  37. .list {
  38. margin-left: 32px;
  39. margin-right: 32px;
  40. margin-top: 30px;
  41. height: 500px;
  42. }
  43. .list-item {
  44. height: 200px;
  45. margin-right: 20px;
  46. margin-bottom: 20px;
  47. }
  48. .btn-container{
  49. height: 100px;
  50. align-items: center;
  51. justify-content: center;
  52. }
  53. @media screen and (max-width: 599) {
  54. .list{
  55. columns: 3;
  56. }
  57. }
  58. @media screen and (min-width: 600) {
  59. .list{
  60. columns: 4;
  61. }
  62. }
  63. </style>
  64. <script>
  65. const orientationMap = {
  66. portrait: 'portrait',
  67. landscape: 'landscape'
  68. }
  69. import configuration from '@system.configuration';
  70. module.exports = {
  71. public: {
  72. orientation: orientationMap.portrait
  73. },
  74. onInit: function () {
  75. this.$page.setTitleBar({ text: this.$t('message.framework.screenSwitch.barTitle') });
  76. },
  77. switchOrientation() {
  78. if (this.orientation === orientationMap.portrait) {
  79. this.orientation = orientationMap.landscape;
  80. } else {
  81. this.orientation = orientationMap.portrait;
  82. }
  83. configuration.setScreenOrientation({
  84. orientation: this.orientation,
  85. })
  86. }
  87. }
  88. </script>

竖屏时效果:

cke_3971.png

横屏时效果:

cke_5632.png

 欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/219088
推荐阅读
相关标签
  

闽ICP备14008679号