Flex布局,绝对定位,层叠布局Stack的详细讲解
Flex布局
Flex布局也叫做:伸缩布局,当子盒子的总和溢出父盒子时默认进行压缩显示
线性布局的原理是基于Flex来设计的。
Flex布局默认主轴是:水平向右的,交叉轴垂:直向下
单行或者单列的情况下,优先使用线性布局
线性布局的性能由于Flex布局
FLex布局我们可以根据 direction 来调整主轴的方向
direction:FlexDirection.Row 主轴水平向右的
direction:FlexDirection.Column 主轴垂直向下的
direction:FlexDirection.RowReverse 主轴水平向左,与Row
direction:FlexDirection.ColumnReverse 垂直从下到上,与Column 相反
Flex布局主轴垂直向下
@Entry
@Component
struct Index {
build() {
Flex({
direction:FlexDirection.Column // 主轴垂直向下的
}){
Text('111').width(80).height(80).backgroundColor("#bab")
Text('222').width(80).height(80).backgroundColor("#d9d")
Text('333').width(80).height(80).backgroundColor("#c2c")
}.width('100%').height("100%")
}
}
Flex布局主轴:垂直从下到上
@Entry
@Component
struct Index {
build() {
Flex({
direction:FlexDirection.ColumnReverse // 垂直从下到上,与Column 相反
}){
Text('111').width(80).height(80).backgroundColor("#bab")
Text('222').width(80).height(80).backgroundColor("#d9d")
Text('333').width(80).height(80).backgroundColor("#c2c")
}.width('100%').height("100%")
}
}
主轴的对齐方式,与之前学习的线性布局方式是一样的哈
justifyContent:FlexAlign.Start在最起始位置;
justifyContent:FlexAlign.Center在中间;
justifyContent:FlexAlign.End 在最末端;
justifyContent:FlexAlign.SpaceBetween 两端紧挨着,中间均匀分布。与css3的一样。
justifyContent:FlexAlign.SpaceAround 两端有一点间距(0.5),中间间距(1)均匀分布。与css3的一样。
justifyContent:FlexAlign.SpaceEvenly 间隙是均匀的。
主轴水平向右,排列方式间隙均匀
@Entry
@Component
struct Index {
build() {
Flex({
direction:FlexDirection.Row, // 主轴是:水平向右的
justifyContent:FlexAlign.SpaceEvenly // 排列方式:间隙均匀
}){
Text('111').width(80).height(80).backgroundColor("#bab")
Text('222').width(80).height(80).backgroundColor("#d9d")
Text('333').width(80).height(80).backgroundColor("#c2c")
}.width('100%').height("100%")
}
}
主轴水平向右,排列方式:两端紧挨着,中间均匀分布
@Entry
@Component
struct Index {
build() {
Flex({
direction:FlexDirection.Row, // 主轴是:水平向右的
justifyContent:FlexAlign.SpaceBetween, // 排列方式:两端紧挨着,中间均匀分布
}){
Text('111').width(80).height(80).backgroundColor("#bab")
Text('222').width(80).height(80).backgroundColor("#d9d")
Text('333').width(80).height(80).backgroundColor("#c2c")
}.width('100%').height("100%")
}
}
交叉轴的对齐方式
alignItems: ItemAlign.Start 顶部
alignItems: ItemAlign.Center 居中
alignItems: ItemAlign.End 底部
alignItems: ItemAlign.Stretch 拉伸,占满整个空间
交叉轴的对齐方式: 垂直方向拉伸,占满整个空间
@Entry
@Component
struct Index {
build() {
Flex({
direction:FlexDirection.Row, // 主轴是:水平向右的
justifyContent:FlexAlign.SpaceBetween, // 排列方式:两端紧挨着,中间均匀分布,
alignItems: ItemAlign.Stretch // 交叉轴的对齐方式: 垂直方向拉伸,占满整个空间
}){
Text('111').width(80).height(80).backgroundColor("#bab")
Text('222').width(80).height(80).backgroundColor("#d9d")
Text('333').width(80).height(80).backgroundColor("#c2c")
}.width('100%').height("100%")
}
}
交叉轴对齐方式:在末端
@Entry
@Component
struct Index {
build() {
Flex({
direction:FlexDirection.Row, // 主轴是:水平向右的
justifyContent:FlexAlign.SpaceBetween, // 排列方式:两端紧挨着,中间均匀分布,
alignItems: ItemAlign.End // 交叉轴方向上:在末端
}){
Text('111').width(80).height(80).backgroundColor("#bab")
Text('222').width(80).height(80).backgroundColor("#d9d")
Text('333').width(80).height(80).backgroundColor("#c2c")
}.width('100%').height("100%")
}
}
是否换行
wrap: FlexWrap.Wrap 换行
wrap: FlexWrap.NoWrap 不换行。在使用Flex布局时,如果不进行换行,子元素的宽度大于父元素。会进行挤压
wrap: FlexWrap.WrapReverse 换行翻转
不换行
在使用Flex布局时,如果不进行换行,子元素的宽度大于父元素。会进行挤压
@Entry
@Component
struct Index {
build() {
Flex({
wrap:FlexWrap.NoWrap
}){
Text('111').width(180).height(80).backgroundColor("#bab")
Text('222').width(180).height(80).backgroundColor("#d9d")
Text('333').width(180).height(80).backgroundColor("#c2c")
Text('444').width(180).height(80).backgroundColor("#ccc")
}.width('100%').height("100%")
}
}
换行显示
@Entry
@Component
struct Index {
build() {
Flex({
wrap:FlexWrap.Wrap
}){
Text('111').width(180).height(80).backgroundColor("#bab")
Text('222').width(180).height(80).backgroundColor("#d9d")
Text('333').width(180).height(80).backgroundColor("#c2c")
}.width('100%').height("100%")
}
}
换行翻转
@Entry
@Component
struct Index {
build() {
Flex({
wrap:FlexWrap.WrapReverse
}){
Text('111').width(180).height(80).backgroundColor("#bab")
Text('222').width(180).height(80).backgroundColor("#d9d")
Text('333').width(180).height(80).backgroundColor("#c2c")
}.width('100%').height("100%")
}
}
实现XX直聘搜索历史记录
@Entry
@Component
struct Index {
build() {
Column(){
Row(){
Text('历史搜索').fontSize(22).fontWeight(700).margin({left:10, bottom:10})
}.width('100%')
Flex({
wrap: FlexWrap.Wrap
}){
Text('galang').height(30).backgroundColor("#ccc").borderRadius(4).padding({left:8, right:8}).margin({left:10,right:10, bottom:10})
Text('go开发工程师').height(30).backgroundColor("#ccc").padding({left:8, right:8}).borderRadius(4).margin({left:10,right:10, bottom:10})
Text('web开发工程师').height(30).backgroundColor("#ccc").padding({left:8, right:8}).borderRadius(4).margin({left:10,right:10, bottom:10})
Text('嵌入式开发').height(30).backgroundColor("#ccc").padding({left:8, right:8}).borderRadius(4).margin({left:10,right:10, bottom:10})
Text('Ai开发').height(30).backgroundColor("#ccc").padding({left:8, right:8}).borderRadius(4).margin({left:10,right:10, bottom:10})
}
}
}
}
绝对定位
绝对定位后的组件不再占用自身原有位置,可以实现层叠效果,原本的位置就不进行占用了。
默认情况下,绝对定位的堆叠层级与组件的加载顺序有关,在最后的组件往往在最上面。
如果使用了ZIndex,就与ZIndex的值大小有关,谁值越大,谁就在最上面。
语法:组件.position({x:数值,y:数值}) 或者 组件.position({right:数值,top:数值})
你也可以这样写:Text('使用定位').position({ right:10,top:0}).backgroundColor(Color.Pink)
@Entry
@Component
struct Index {
build() {
Flex({
wrap:FlexWrap.NoWrap
}){
Text('我使用了绝对定位').position({ right:10,top:0}).backgroundColor(Color.Pink)
}.width('100%').height("100%")
}
}
ZIndex的使用
@Entry
@Component
struct Index {
build() {
Flex({
wrap:FlexWrap.NoWrap
}){
Text('最上面').position({ right:20,top:50}).backgroundColor('#ccc').width(60).height(40).zIndex(10)
Text('哈哈').position({ right:10,top:0}).backgroundColor(Color.Pink).width(200).height(200)
Text('五一结束啦').position({ right:20,top:50}).backgroundColor(Color.Orange).width(100).height(100)
}.width('100%').height("100%")
}
}
层叠布局
层叠布局:堆叠容器,子组件按照顺序依次入栈,后一个子组件覆盖前一个子组件。
层叠布局具有较强的组件层叠能力。
场景:卡片层叠效果等,如购物车等
特点:层叠操作更简洁,编码效率高。(绝对定位的优势是更灵活)
我们可以通过alignContent来控制显示的位置
语法:Stack({alignContent:Alignment.Start}){ })
alignContent的属性值通过有下面这9个属性值
值 | 说明 |
---|---|
Alignment.TopStart | 子组件对齐到容器的左上角(Top-Left) |
Alignment.Top | 子组件水平居中,并紧贴容器的顶部 |
Alignment.TopEnd | 子组件对齐到容器的右上角(Top-Right) |
Alignment.Start | 子组件垂直居中,并紧贴容器的左侧(Left) |
Alignment.Center | 子组件在容器的中心位置(水平和垂直居中) |
Alignment.End | 子组件垂直居中,并紧贴容器的右侧(Right) |
Alignment.BottomStart | 子组件对齐到容器的左下角(Bottom-Left) |
Alignment.Bottom | 子组件水平居中,并紧贴容器的底部 |
Alignment.BottomEnd | 子组件对齐到容器的右下角(Bottom-Right) |
层叠布局: alignContent:Alignment.Start 子组件垂直居中,并紧贴容器的左侧
@Entry
@Component
struct Index {
build() {
Stack({alignContent:Alignment.Start}){
Text("最大").width(200).height(200).backgroundColor(Color.Pink)
Text("中等").width(120).height(120).backgroundColor('#50D')
Text("最小").width(40).height(40).backgroundColor('#FF3')
}.width('100%').height('100%').backgroundColor('#ccc')
}
}
层叠布局: alignContent:Alignment.TopEnd 右上角
@Entry
@Component
struct Index {
build() {
Stack({alignContent:Alignment.TopEnd}){
Text("最大").width(200).height(200).backgroundColor(Color.Pink)
Text("中等").width(120).height(120).backgroundColor('#50D')
Text("最小").width(40).height(40).backgroundColor('#FF3')
}.width('100%').height('100%').backgroundColor('#ccc')
}
}
层叠布局: alignContent:Alignment.BottomStart 左边底部
@Entry
@Component
struct Index {
build() {
Stack({alignContent:Alignment.BottomStart}){
Text("最大").width(200).height(200).backgroundColor(Color.Pink)
Text("中等").width(120).height(120).backgroundColor('#50D')
Text("最小").width(40).height(40).backgroundColor('#FF3')
}.width('100%').height('100%').backgroundColor('#ccc')
}
}
通过ZIndex来控制层级关系
@Entry
@Component
struct Index {
build() {
Stack({alignContent:Alignment.Center}){
Text("最大").width(200).height(200).backgroundColor(Color.Pink).zIndex(40)
Text("中等").width(120).height(120).backgroundColor('#50D').zIndex(20)
Text("最小").width(40).height(40).backgroundColor('#FF3').zIndex(50)
}.width('100%').height('100%').backgroundColor('#ccc')
}
}
层叠布局和绝对定位的区别
层叠布局(Stack):通过对齐属性快速实现组件堆叠,适合简单叠加场景。
绝对定位:通过精确坐标自由控制位置,适合复杂或动态布局需求。
也就是说:绝对定位更加控制位置更加的自由。
能够使用层叠布局实现的,一定可以使用绝对定位来实现。
在我们实现需求时,优先使用:层叠布局,如果不好实现的话,在考虑绝对定位
Flex布局,绝对定位,层叠布局Stack的详细讲解的更多相关文章
- Flutter 布局类组件:层叠布局(Stack和Positioned)
前言 层叠布局,即子组件可以根据距父容器四个角的位置来确定自身的位置.绝对定位运行子组件堆叠起来,即按照代码中声明的顺序. Flutter中使用Stack和Positioned这两个组件来配合实现绝对 ...
- css-前端实现左中右三栏布局的常用方法:绝对定位,圣杯,双飞翼,flex,table-cell,网格布局等
1.前言 作为一个前端开发人员,工作学习中经常会遇到快速构建网页布局的情况,这篇我整理了一下我知道的一些方法.我也是第一次总结,包括圣杯布局,双飞翼布局,table-cell布局都是第一次听说,可能会 ...
- Flutter 布局(八)- Stack、IndexedStack、GridView详解
本文主要介绍Flutter布局中的Stack.IndexedStack.GridView控件,详细介绍了其布局行为以及使用场景,并对源码进行了分析. 1. Stack A widget that po ...
- CSS3 Flex布局(伸缩布局盒模型)学习
CSS3 Flex布局(伸缩布局盒模型)学习 转自:http://www.xifengxx.com/web-front-end/1408.html CSS2定义了四种布局:块布局.行内布局.表格布局盒 ...
- 技术胖Flutter第三季-16Stack层叠布局
16Stack层叠布局 在上面声明一个变量Stack里面包含两个元素,第一个 是CircleAvater第二个子对象是Container 效果 把文字房子啊中下的位置: 我们需要对齐属性 包含了x轴和 ...
- 技术胖Flutter第三季-17布局PositionedWidget层叠定位组件
博客地址: https://jspang.com/post/flutter3.html#toc-d7a 把我们上节的 Container的部分代码去掉. 使用:Positioned 有点像css里面的 ...
- CSS的flex布局和Grid布局
一.什么是 flex 布局 2009年,W3C 提出了一种新的方案----Flex 布局,可以简便.完整.响应式地实现各种页面布局.目前,它已经得到了所有浏览器的支持,这意味着,现在就能很安全地使用这 ...
- CSS3总结五:弹性盒子(flex)、弹性盒子布局
弹性盒子容器的属性与应用 display:flex/inline-flex flex-direction flex-wrap justify-content align-items align-con ...
- 前端(八)—— 高级布局:文档流、浮动布局、流式布局、定位布局、flex布局、响应布局
高级布局:文档流.浮动布局.流式布局.定位布局.flex布局.响应布局 一.文档流 1.什么是文档流 将窗体自上而下分成一行一行,块级元素从上至下.行内元素在每行中从左至右的顺序依次排放元素 2.本质 ...
- Flex布局【弹性布局】学习
先让我们看看在原来的学习中遇到的问题 之前在软件工程的大作业中,自己从零开始学习如何开发一个网站,从页面,到后台,当然数据库是大二的必修课 在学习如何编写一个静态页面的时候,完全是自学,自己摸索,所以 ...
随机推荐
- 堆排序(topk 问题)(NB)
博客地址:https://www.cnblogs.com/zylyehuo/ # _*_coding:utf-8_*_ # 比较排序 import random def sift(li, low, h ...
- 【服务器】npm配置国内镜像源
[服务器]npm配置国内镜像源 零.问题 配置Node.js的HTTPS的时候,下载不了 一.解决 这里使用的是淘宝的镜像: npm config set registry https://regis ...
- useDeferredValue的作用
前言 useDeferredValue是react18新增的一个用于优化性能的一个hook,它的作用是延迟获取一个值,实际开发中的常规用法与我们之前所用的防抖和节流很相似,但是也有一定的区别.本篇文章 ...
- RESTful的连接时间超时时间设定
dsResrful的连接方式时,如何设定timeout呢? DSRestConnection.HTTP.ConnectTimeout := 5000; 就这么简单.因为封装的indy的TidHTTP. ...
- devops 2024
What is DevOps? DevOps is a mindset, a culture, and a set of technical practices. It provides commun ...
- nodejs终端字符样式和进度条
Nodejs为终端字符增加样式 只有黑白的色调对于比较复杂的命令行程序来说就显得太单调了,我们可以为命令行程序增加样式使得程序更加友好! 安装package: npm install -S chalk ...
- 静态批处理/动态批处理/GPU Instancing /SRP Batcher的详细剖析
静态批处理[1] 定义 标明为 Static 的静态物件,如果在使用相同材质球的条件下,在Build(项目打包)的时候Unity会自动地提取这些共享材质的静态模型的Vertex buffer和Inde ...
- 创建bean对象的三种方式
一.使用无参构造方法创建 二.使用静态工厂创建 三.使用实例工厂创建
- 【记录】Opencv+LibTorch安装
环境Ubuntu20.04. [Opencv]源码编译:Linux系统下opencv3.4.1安装教程 [LibTorch]下载已编译版本:在Ubuntu下安装LibTorch [磁盘空间不足]vmw ...
- MySQL之"数据库中没有就创建,有就修改"ON DUPLICATE KEY UPDATE
一.场景 当你想存入一条数据到扩展表中(主表下附表),但这个扩展表并非一定会创建,就会让其工程逻辑复杂化 (也就是说:有可能创建主表数据的同时不会创建扩展表数据,这样就会照成你想修改的时候,扩展表本身 ...