RESPONSIVE_LAYOUT_EXAMPLE.md 7.79 KB

响应式布局使用示例

示例 1:基础响应式文本

<template>
    <view class="container">
        <text :style="{ fontSize: fontSize + 'px', padding: padding + 'px' }">
            这是响应式文本,会根据屏幕尺寸自动调整大小
        </text>
    </view>
</template>

<script setup lang="uts">
import { ref, onMounted } from 'vue'
import { responsiveFontSize, responsiveSpacing } from '@/utils/screen-adapt.uts'

const fontSize = ref<number>(14)
const padding = ref<number>(16)

onMounted(() => {
    // 基础字体 14px(设计稿 375px),最小 12px,最大 20px
    fontSize.value = responsiveFontSize(14, 12, 20)

    // 基础间距 16px(设计稿 375px),最小 12px,最大 24px
    padding.value = responsiveSpacing(16, 12, 24)
})
</script>

<style>
.container {
    display: flex;
    flex-direction: column;
    padding: 16px;
}
</style>

示例 2:响应式列表布局(手机单列,平板多列)

<template>
    <view :class="['list-container', `list-${screenSize}`]">
        <view class="list-item" v-for="item in list" :key="item.id">
            <text class="item-title">{{ item.title }}</text>
            <text class="item-desc">{{ item.desc }}</text>
        </view>
    </view>
</template>

<script setup lang="uts">
import { ref, onMounted } from 'vue'
import { getScreenSize, responsiveFontSize, responsiveSpacing } from '@/utils/screen-adapt.uts'

const screenSize = ref<'small' | 'medium' | 'large' | 'xlarge'>('medium')
const list = ref([
    { id: 1, title: '标题1', desc: '描述1' },
    { id: 2, title: '标题2', desc: '描述2' },
    { id: 3, title: '标题3', desc: '描述3' }
])

onMounted(() => {
    screenSize.value = getScreenSize()
})
</script>

<style>
.list-container {
    display: flex;
    flex-wrap: wrap;
    padding: 16px;
}

.list-item {
    background-color: #f5f5f5;
    border-radius: 8px;
    padding: 16px;
    margin-bottom: 16px;
}

/* 手机:单列 */
.list-small .list-item,
.list-medium .list-item {
    width: 100%;
}

/* 大屏手机/小平板:两列 */
.list-large .list-item {
    width: calc(50% - 8px);
    margin-right: 16px;
}

.list-large .list-item:nth-child(2n) {
    margin-right: 0;
}

/* 平板:三列 */
.list-xlarge .list-item {
    width: calc(33.333% - 11px);
    margin-right: 16px;
}

.list-xlarge .list-item:nth-child(3n) {
    margin-right: 0;
}

.item-title {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 8px;
}

.item-desc {
    font-size: 14px;
    color: #666;
}
</style>

示例 3:使用 Composable 封装

<template>
    <view :class="containerClass">
        <text :style="textStyle">响应式文本</text>
        <view class="info">
            <text>屏幕宽度:{{ screenWidth }}px</text>
            <text>屏幕尺寸:{{ screenSize }}</text>
            <text>是否平板:{{ isTabletMode ? '是' : '否' }}</text>
        </view>
    </view>
</template>

<script setup lang="uts">
import { computed } from 'vue'
import { useResponsive } from '@/composables/useResponsive.uts'

const { screenWidth, screenSize, isTabletMode, fontSize, spacing } = useResponsive()

// 动态样式对象
const textStyle = computed(() => {
    return {
        fontSize: fontSize.value(14, 12, 20) + 'px',
        padding: spacing.value(16, 12, 24) + 'px',
        marginBottom: spacing.value(8, 4, 16) + 'px'
    }
})

// 动态 class
const containerClass = computed(() => {
    return `container container-${screenSize.value} ${isTabletMode.value ? 'tablet' : 'mobile'}`
})
</script>

<style>
.container {
    display: flex;
    flex-direction: column;
    padding: 16px;
}

.info {
    display: flex;
    flex-direction: column;
    margin-top: 16px;
}

.info text {
    font-size: 12px;
    color: #666;
    margin-bottom: 4px;
}
</style>

示例 4:响应式卡片网格

<template>
    <view :class="['card-grid', `grid-${screenSize}`]">
        <view class="card" v-for="card in cards" :key="card.id">
            <image class="card-image" :src="card.image" mode="aspectFill" />
            <view class="card-content">
                <text class="card-title">{{ card.title }}</text>
                <text class="card-desc">{{ card.desc }}</text>
            </view>
        </view>
    </view>
</template>

<script setup lang="uts">
import { ref, onMounted } from 'vue'
import { getScreenSize, getColumns } from '@/utils/screen-adapt.uts'

const screenSize = ref<'small' | 'medium' | 'large' | 'xlarge'>('medium')
const columns = ref<number>(1)
const cards = ref([
    { id: 1, title: '卡片1', desc: '描述1', image: '/static/image1.jpg' },
    { id: 2, title: '卡片2', desc: '描述2', image: '/static/image2.jpg' },
    { id: 3, title: '卡片3', desc: '描述3', image: '/static/image3.jpg' }
])

onMounted(() => {
    screenSize.value = getScreenSize()
    columns.value = getColumns(1, 3) // 手机1列,平板3列
})
</script>

<style>
.card-grid {
    display: flex;
    flex-wrap: wrap;
    padding: 16px;
    gap: 16px; /* 注意:uni-app x 可能不支持 gap,改用 margin */
}

/* 如果 gap 不支持,使用以下方式 */
.card-grid {
    display: flex;
    flex-wrap: wrap;
    padding: 16px;
    margin: -8px; /* 负 margin 抵消子元素 margin */
}

.card {
    background-color: #fff;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    margin: 8px; /* 子元素 margin */
}

/* 手机:单列 */
.grid-small .card,
.grid-medium .card {
    width: calc(100% - 16px);
}

/* 大屏手机/小平板:两列 */
.grid-large .card {
    width: calc(50% - 16px);
}

/* 平板:三列 */
.grid-xlarge .card {
    width: calc(33.333% - 16px);
}

.card-image {
    width: 100%;
    height: 200px;
    background-color: #f0f0f0;
}

.card-content {
    padding: 16px;
}

.card-title {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 8px;
}

.card-desc {
    font-size: 14px;
    color: #666;
}
</style>

示例 5:响应式表单布局

<template>
    <view :class="['form-container', `form-${screenSize}`]">
        <view class="form-item" v-for="field in formFields" :key="field.id">
            <text class="form-label">{{ field.label }}</text>
            <input class="form-input" :placeholder="field.placeholder" />
        </view>
        <button class="form-submit">提交</button>
    </view>
</template>

<script setup lang="uts">
import { ref, onMounted } from 'vue'
import { getScreenSize, responsiveFontSize, responsiveSpacing } from '@/utils/screen-adapt.uts'

const screenSize = ref<'small' | 'medium' | 'large' | 'xlarge'>('medium')
const formFields = ref([
    { id: 1, label: '姓名', placeholder: '请输入姓名' },
    { id: 2, label: '邮箱', placeholder: '请输入邮箱' },
    { id: 3, label: '电话', placeholder: '请输入电话' }
])

onMounted(() => {
    screenSize.value = getScreenSize()
})
</script>

<style>
.form-container {
    display: flex;
    flex-direction: column;
    padding: 16px;
}

/* 手机:单列 */
.form-small .form-item,
.form-medium .form-item {
    width: 100%;
    margin-bottom: 16px;
}

/* 平板:两列 */
.form-large .form-item,
.form-xlarge .form-item {
    width: calc(50% - 8px);
    margin-right: 16px;
    margin-bottom: 16px;
}

.form-large .form-item:nth-child(2n),
.form-xlarge .form-item:nth-child(2n) {
    margin-right: 0;
}

.form-label {
    font-size: 14px;
    margin-bottom: 8px;
    color: #333;
}

.form-input {
    width: 100%;
    height: 40px;
    padding: 0 12px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 14px;
}

.form-submit {
    width: 100%;
    height: 44px;
    background-color: #007aff;
    color: #fff;
    border-radius: 4px;
    margin-top: 16px;
}
</style>

注意事项

  1. gap 属性:uni-app x 可能不支持 gap,使用 margin 替代(容器负 margin + 子元素 margin)

  2. calc() 函数:uni-app x 不支持 calc(),使用 flex + 百分比 + margin 替代

  3. 性能优化:

    • 屏幕信息获取在 onMounted 时执行一次
    • 使用 ref 缓存计算结果
    • 避免在模板中频繁调用函数
  4. 设计稿基准:

    • 统一设计稿基准宽度(如 375px)
    • 所有计算基于同一基准
  5. 测试覆盖:

    • 测试不同屏幕尺寸(320px、375px、414px、768px、1024px)
    • 测试横屏模式
    • 测试平板设备

文档版本:v1.0
创建日期:2026-01-23