uniApp中uView组件库的丰富布局方法

目录

基本使用

#分栏间隔

#混合布局

#分栏偏移

#对齐方式

API

#Row Props

#Col Props

#Row Events

#Col Events


UniApp的uView组件库是一个丰富的UI组件库,提供了各种常用的UI组件和布局方法,帮助开发者快速构建美观、灵活的界面。下面给你写一篇关于uView组件库的布局方法的博客:

基本使用

通过col组件的span设置需要分栏的比例

<template>
    <view class="u-page">
        <view class="u-demo-block">
            <text class="u-demo-block__title">基础使用</text>
            <view class="u-demo-block__content">
                <u-row customStyle="margin-bottom: 10px">
                    <u-col span="6">
                        <view class="demo-layout bg-purple-light"></view>
                    </u-col>
                    <u-col span="6">
                        <view class="demo-layout bg-purple"></view>
                    </u-col>
                </u-row>
                <u-row customStyle="margin-bottom: 10px">
                    <u-col span="4">
                        <view class="demo-layout bg-purple"></view>
                    </u-col>
                    <u-col span="4">
                        <view class="demo-layout bg-purple-light"></view>
                    </u-col>
                    <u-col span="4">
                        <view class="demo-layout bg-purple-dark"></view>
                    </u-col>
                </u-row>
                <u-row justify="space-between">
                    <u-col span="3">
                        <view class="demo-layout bg-purple"></view>
                    </u-col>
                    <u-col span="3">
                        <view class="demo-layout bg-purple-light"></view>
                    </u-col>
                    <u-col span="3">
                        <view class="demo-layout bg-purple"></view>
                    </u-col>
                    <u-col span="3">
                        <view class="demo-layout bg-purple-light"></view>
                    </u-col>
                </u-row>
            </view>
        </view>
    </view>
</template>

<style lang="scss">
    .wrap {
        padding: 12px;
    }

    .demo-layout {
        height: 25px;
        border-radius: 4px;
    }

    .bg-purple {
        background: #CED7E1;
    }

    .bg-purple-light {
        background: #e5e9f2;
    }

    .bg-purple-dark {
        background: #99a9bf;
    }
</style>

#分栏间隔

通过设置row组件的gutter参数,来指定每一栏之间的间隔(最终表现为左边内边距各为gutter/2),默认间隔为0

<view class="u-demo-block__content">
    <u-row
            justify="space-between"
            gutter="10"
    >
        <u-col span="3">
            <view class="demo-layout bg-purple"></view>
        </u-col>
        <u-col span="3">
            <view class="demo-layout bg-purple-light"></view>
        </u-col>
        <u-col span="3">
            <view class="demo-layout bg-purple"></view>
        </u-col>
        <u-col span="3">
            <view class="demo-layout bg-purple-light"></view>
        </u-col>
    </u-row>
</view>

<style lang="scss">
    .wrap {
        padding: 12px;
    }

    .demo-layout {
        height: 25px;
        border-radius: 4px;
    }

    .bg-purple {
        background: #CED7E1;
    }

    .bg-purple-light {
        background: #e5e9f2;
    }

    .bg-purple-dark {
        background: #99a9bf;
    }
</style>

#混合布局

通过指定col组件的span属性,指定不同的值,达到不同的比例

<view class="u-demo-block__content">
    <u-row
        justify="space-between"
        gutter="10"
    >
        <u-col span="2">
            <view class="demo-layout bg-purple-light"></view>
        </u-col>
        <u-col span="4">
            <view class="demo-layout bg-purple"></view>
        </u-col>
        <u-col span="6">
            <view class="demo-layout bg-purple-dark"></view>
        </u-col>
    </u-row>
</view>

<style lang="scss">
    .wrap {
        padding: 12px;
    }

    .demo-layout {
        height: 25px;
        border-radius: 4px;
    }

    .bg-purple {
        background: #CED7E1;
    }

    .bg-purple-light {
        background: #e5e9f2;
    }

    .bg-purple-dark {
        background: #99a9bf;
    }
</style>

#分栏偏移

通过指定col组件的offset属性可以指定分栏偏移的栏数。

<view class="u-demo-block__content">
    <u-row
            justify="space-between"
            customStyle="margin-bottom: 10px"
    >
        <u-col
                span="3"
                offset="3"
        >
            <view class="demo-layout bg-purple-light"></view>
        </u-col>
        <u-col
                span="3"
                offset="3"
        >
            <view class="demo-layout bg-purple"></view>
        </u-col>
    </u-row>
    <u-row>
        <u-col span="3">
            <view class="demo-layout bg-purple-light"></view>
        </u-col>
        <u-col
                span="3"
                offset="3"
        >
            <view class="demo-layout bg-purple"></view>
        </u-col>
    </u-row>
</view>

#对齐方式

通过row组件的justify来对分栏进行灵活的对齐, 可选值为start(或flex-start)、end(或flex-end)、centeraround(或space-around)、between(或space-between), 其最终的表现类似于css的justify-content属性。

注意:由于持微信小程序编译后的特殊结构,此方式不支持微信小程序。

<view class="u-demo-block__content">
    <u-row
            justify="space-between"
            customStyle="margin-bottom: 10px"
    >
        <u-col
                span="3"
        >
            <view class="demo-layout bg-purple-light"></view>
        </u-col>
        <u-col
                span="3"
        >
            <view class="demo-layout bg-purple"></view>
        </u-col>
    </u-row>
    <u-row>
        <u-col span="3">
            <view class="demo-layout bg-purple-light"></view>
        </u-col>
        <u-col
                span="3"
        >
            <view class="demo-layout bg-purple"></view>
        </u-col>
    </u-row>
</view>

API

#Row Props

参数 说明 类型 默认值 可选值
gutter 栅格间隔,左右各为此值的一半,单位任意 String | Number 0 -
justify 水平排列方式(微信小程序暂不支持) String start(或flex-start) end(或flex-end) / center / around(或space-around) / between(或space-between)
align 垂直排列方式 String center top / bottom

#Col Props

参数 说明 类型 默认值 可选值
span 栅格占据的列数,总12等分 String | Number 0 1-12
offset 分栏左边偏移,计算方式与span相同 String | Number 0 -
justify 水平排列方式 String start start(或flex-start)、end(或flex-end)、centeraround(或space-around)、between(或space-between)
align 垂直对齐方式 String stretch topcenterbottomstretch
textAlign 文字水平对齐方式 String left center / right

#Row Events

事件名 说明 回调参数
click row被点击 -

#Col Events

事件名 说明 回调参数
click col被点击,会阻止事件冒泡到row -

总的来说,uView组件库提供了丰富的布局方法和UI组件,可以帮助UniApp开发者快速构建出美观、灵活的界面布局,提升开发效率,为用户提供更加舒适的交互体验。开发者可以根据项目需求选择合适的uView组件,轻松实现各种复杂的布局效果。

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-28 15:12:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-28 15:12:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-28 15:12:02       82 阅读
  4. Python语言-面向对象

    2023-12-28 15:12:02       91 阅读

热门阅读

  1. 计算机网络的7层协议和4层协议的区别

    2023-12-28 15:12:02       57 阅读
  2. Linux:环境变量

    2023-12-28 15:12:02       61 阅读
  3. 2024 年软件工程将如何发展

    2023-12-28 15:12:02       47 阅读
  4. leetcode18. 四数之和

    2023-12-28 15:12:02       59 阅读
  5. go语言10进制与16进制转换

    2023-12-28 15:12:02       56 阅读
  6. 关于:网络安全

    2023-12-28 15:12:02       56 阅读
  7. mysql导入tab符号txt文件到数据库

    2023-12-28 15:12:02       52 阅读
  8. Vue.js

    Vue.js

    2023-12-28 15:12:02      41 阅读
  9. 如何判断网站服务器的访问承载量?

    2023-12-28 15:12:02       54 阅读
  10. html隐藏元素的方法

    2023-12-28 15:12:02       55 阅读
  11. Tomcat使用手册

    2023-12-28 15:12:02       45 阅读
  12. CSS标准盒子模型和怪异盒子模型

    2023-12-28 15:12:02       53 阅读
  13. 自然语言处理(NLP)技术

    2023-12-28 15:12:02       59 阅读
  14. Linux用wget/curl 发起post请求

    2023-12-28 15:12:02       57 阅读