README-layout.md
3.03 KB
布局与缩放系统
概述
- 模式:portrait / pad / desktop(当前仅 pad,预留扩展)
- 缩放:同模式下不同分辨率等比缩放,避免拉伸
- 计算:App 启动时算一次,缓存供全局使用
屏幕方向(App)
-
pages.json:已在globalStyle设置pageOrientation": "auto"(Android 4.13+ 起生效)。若保持默认portrait,仅调FULL_SENSOR往往仍无法横屏,因 uni-app x 页面层按竖屏约束。 -
本工程策略:
App.onLaunch/App.onShow:applyScreenOrientationLock()。小屏手持机:非layoutService.isPadLikeDevice()且长边 < 1024(不依赖deviceType===phone,避免厂商未上报导致从不锁屏)→ Android 使用SENSOR_PORTRAIT+ 多次补锁;LayoutRoot在窗口 宽>高 时再调reassertHandsetPortraitIfNeeded()。Pad/大屏:unlockOrientation(FULL_SENSOR)+ 延迟再 unlock。 - 布局分档(portrait / pad / desktop)仍由
layoutService在 init 时根据getDeviceInfo+ 尺寸兜底计算,与上述「是否锁屏」独立。控制台[orientation-diag]、[orientation-lock]、[plus-screen]可排查。
使用方式
1. 页面使用布局变量
页面根节点用 layout-root 包裹,子组件即可使用 var(--l-xxx):
<template>
<layout-root>
<view class="page">
<view class="item">内容</view>
</view>
</layout-root>
</template>
2. 样式使用变量
.item {
padding: var(--l-spacing-md);
margin-bottom: var(--l-spacing-lg);
font-size: var(--l-font-base);
border-radius: var(--l-radius-md);
}
3. 变量列表
| 变量 | 说明 | 默认值(1024基准) |
|---|---|---|
| --l-spacing-xs | 间距 | 8px |
| --l-spacing-sm | 间距 | 12px |
| --l-spacing-md | 间距 | 16px |
| --l-spacing-lg | 间距 | 24px |
| --l-spacing-xl | 间距 | 32px |
| --l-spacing-2xl | 间距 | 40px |
| --l-font-xs | 字体 | 12px |
| --l-font-sm | 字体 | 14px |
| --l-font-base | 字体 | 16px |
| --l-font-lg | 字体 | 18px |
| --l-font-xl | 字体 | 20px |
| --l-font-2xl | 字体 | 24px |
| --l-radius-sm | 圆角 | 4px |
| --l-radius-md | 圆角 | 8px |
| --l-radius-lg | 圆角 | 12px |
| --l-radius-xl | 圆角 | 16px |
4. JS 中按设计稿缩放
import { useLayout } from '@/composables/useLayout'
const { scaleSize } = useLayout()
// 设计稿 200px 宽的按钮
const btnWidth = scaleSize(200)
5. 模式判断(预留)
const { isPad, isPortrait, isDesktop } = useLayout()
if (isPad.value) {
// pad 布局
}
扩展 portrait / desktop
修改 services/LayoutService.uts 的 detectMode():
private detectMode(): LayoutMode {
const w = this.screenWidth.value
const isPortrait = this.screenHeight.value > this.screenWidth.value
if (isPortrait && w <= 768) return 'portrait'
if (w >= 1440) return 'desktop'
return 'pad'
}
并在 config/layout.config.uts 中完善各模式的 baseWidth/baseHeight。