React-native之Flexbox

article/2025/6/8 14:46:15

本文总结:

我们学到了 React Native 的 Flexbox 布局,它让写样式变得更方便啦!😊 Flexbox 就像一个有弹性的盒子,有主轴和交叉轴(行或列)。

在 RN 里写样式要用 StyleSheet.create 对象,属性名是驼峰命名

文章还介绍了如何用 Platform.select 给不同平台写样式,以及使用 styled-components 这个库的简单例子(虽然我们主要还是用 StyleSheet)。

文章还通过三栏布局横向布局的例子,展示了 flexDirection, alignItems, justifyContent alignSelf 这些属性的用法。

特别喜欢 alignSelf: 'stretch' 能让元素填充空间的技巧!👍

最后,通过抽象出 RowCol 组件,我看到了如何用 Flexbox 实现更复杂的嵌套布局。感觉 Flexbox 真的让布局变得灵活多了!🚀

1、关于Flexbox

在将flexbox引入css前,构建布局的各种css属性都比较粗糙,而且很容易出错。而flexbox通过抽象了很多属性来解决问题。字如其名flexbox的意思就是一个具有弹性的盒子模型。我们画个图:假设你有一个容器和它的子元素,它看起来可以是这样的。

Flexbox容器有二个轴,即(向上/向下)或(左/右)。

2、实操

我们直接上代码吧,这是一个js模块,而不是css模块。如果我们要声明rn的样式,我们需要去定义一个对象然后调用StyleSheet.create()最后抛出它在你的模块中。但是我们需要注意的是rn只支持驼峰命名

import { Platform, StyleSheet, StatusBar } from 'react-native'
export default StyleSheet.create({container: {flex: 1,justifyContent: 'center',alignItems: 'center',backgroundColor: 'ghostwhite',...Platform.select({ios: { paddingTop: 20 },android: { paddingTop: StatusBar.currentHeight },}),},box: {width: 100,height: 100,justifyContent: 'center',alignItems: 'center',backgroundColor: 'lightgray',},boxText: {color: 'darkslategray',fontWeight: 'bold',},
})

然后我们看到刚才我写了这样一段代码,这里其实就是根据你的移动端去选择样式。

...Platform.select({ios: { paddingTop: 20 },android: { paddingTop: StatusBar.currentHeight },
}),

ok,我们看一下在rn组件中如何使用

import { Text, View } from 'react-native'
import styles from './styles'
export default function App() {return (<View style={styles.container}><View style={styles.box}><Text style={styles.boxText}>I'm in a box</Text></View></View>)
}

这些样式将通过样式属性分配给每个组件。我们来看看它的表现。

3、Styled-components样式组件库使用

Styled-components是一个css-in-js的库为我们的组件去提供样式,如我们去直接看看怎么使用吧。当然只是介绍一下,我们还是使用styleSheet

首先下载依赖


yarn add styled-components

然后我们写点代码

import styled from "styled-components/native";
const Box = styled.View'width: 100px;height: 100px;justify-content: center;align-items: center;background-color: lightgray;
';
const BoxText = styled.Text'color: darkslategray;font-weight: bold;
';

使用

const App = () => {
return (<Box><BoxText>I'm in a box</BoxText></Box>
);};

4、基础Flexbox

接下来主要讲一下rn中的几种常见布局和flexbox的实战

4.1、三栏布局

普通的从上到下的三栏布局。

你可以把view理解成div,text理解成p

import { Text, View } from 'react-native'
import styles from './styles'
export default function App() {return (<View style={styles.container}><View style={styles.box}><Text style={styles.boxText}>#1</Text></View><View style={styles.box}><Text style={styles.boxText}>#2</Text></View><View style={styles.box}><Text style={styles.boxText}>#3</Text></View></View>)
}

flexDirection属性是决定了主轴的方向,上到下或者左到右,而alignItemjustifyContent属性决定了元素的排列和间隔。


import { Platform, StyleSheet, StatusBar } from 'react-native'
export default StyleSheet.create({container: {flex: 1,flexDirection: 'column',alignItems: 'center',justifyContent: 'space-around',backgroundColor: 'ghostwhite',...Platform.select({ios: { paddingTop: 20 },android: { paddingTop: StatusBar.currentHeight },}),},box: {width: 300,height: 100,justifyContent: 'center',alignItems: 'center',backgroundColor: 'lightgray',borderWidth: 1,borderStyle: 'dashed',borderColor: 'darkslategray',},boxText: {color: 'darkslategray',fontWeight: 'bold',},
})

而如果我们想让它左右两边填满那?就像这样

我们可以加入alignSelf这个属性,这个属性的意思是根据主轴flexDirection的方向,改变宽度或者高度(column改变的就是宽度,row改变的就是高度)去填充空白,动态计算高度或宽度。像这样就是会填满你屏幕的宽度。

  box: {height: 100,justifyContent: 'center',// alignSelf: 'stretch',alignItems: 'center',backgroundColor: 'lightgray',borderWidth: 1,borderStyle: 'dashed',borderColor: 'darkslategray',},

当我们把手机横过去

我们稍微优化一下,正好也写一下横向的布局,上面的样式太`抽象`了写得。

 

import { Text, View, StatusBar } from 'react-native'
import styles from './styles'
import Box from './Box'
export default function App() {return (<View style={styles.container}><Box>#1</Box><Box>#2</Box></View>)
}

import { PropTypes } from 'prop-types'
import { View, Text } from 'react-native'
import styles from './styles'
export default function Box({ children }) {return (<View style={styles.box}><Text style={styles.boxText}>{children}</Text></View>)
}
Box.propTypes = {children: PropTypes.node.isRequired,
}

这个就是会拉伸至整个屏幕高度的横向布局

 

import { Platform, StyleSheet, StatusBar } from 'react-native'
export default StyleSheet.create({container: {flex: 1,flexDirection: 'row',backgroundColor: 'ghostwhite',alignItems: 'center',justifyContent: 'space-around',...Platform.select({ios: { paddingTop: 20 },android: { paddingTop: StatusBar.currentHeight },}),},box: {width: 100,justifyContent: 'center',alignSelf: 'stretch',alignItems: 'center',backgroundColor: 'lightgray',borderWidth: 1,borderStyle: 'dashed',borderColor: 'darkslategray',},boxText: {color: 'darkslategray',fontWeight: 'bold',},
})

我们来看看它的表现

当我们把手机横过去

5、稍微复杂一点的flexBox

假设我们要实现这样一个效果,我们该如何实现?

在我们的意识中,整个布局有。那我们同样也可以抽象出colrow组件分别代表行列。我们直接上代码吧。

可以看到我们分别确定了colrow的方向和排列。

import { Platform, StyleSheet, StatusBar } from 'react-native'export default StyleSheet.create({container: {flex: 1,flexDirection: 'column',backgroundColor: 'ghostwhite',alignItems: 'center',justifyContent: 'space-around',...Platform.select({ios: { paddingTop: 40 },android: { paddingTop: StatusBar.currentHeight },}),},box: {height: 100,width: 100,justifyContent: 'center',alignItems: 'center',borderWidth: 1,borderStyle: 'dashed',borderColor: 'darkslategray',backgroundColor: 'lightgray',},boxText: {color: 'darkslategray',fontWeight: 'bold',},row: {flex: 1,flexDirection: 'row',justifyContent: 'space-around',alignSelf: 'stretch',},column: {flex: 1,flexDirection: 'column',alignItems: 'center',justifyContent: 'space-around',alignSelf: 'stretch',},
})

组件部分

app

import { View, StatusBar } from 'react-native'
import styles from './styles'
import Row from './Row'
import Col from './Col'
import Box from './Box'
export default function App() {return (<View style={styles.container}><StatusBar hidden={false} /><Row><Col><Box>#1</Box><Box>#2</Box></Col><Col><Box>#3</Box><Box>#4</Box></Col></Row><Row><Col><Box>#5</Box><Box>#6</Box></Col><Col><Box>#7</Box><Box>#8</Box></Col></Row><Row><Col><Box>#9</Box><Box>#10</Box></Col><Col><Box>#11</Box><Box>#12</Box></Col></Row></View>)
}

col


import PropTypes from 'prop-types'
import { View } from 'react-native'
import styles from './styles'
export default function Column({ children }) {return <View style={styles.column}>{children}</View>
}Column.propTypes = {children: PropTypes.node.isRequired,
}

row


import PropTypes from 'prop-types'
import { View } from 'react-native'
import styles from './styles'
export default function Row({ children }) {return <View style={styles.row}>{children}</View>
}Row.propTypes = {children: PropTypes.node.isRequired,
}

import React from 'react'
import PropTypes from 'prop-types'
import { View, Text } from 'react-native'
import styles from './styles'
export default function Box({ children }) {return (<View style={styles.box}><Text style={styles.boxText}>{children}</Text></View>)
}Box.propTypes = {children: PropTypes.node.isRequired,
}

6、总结

欢迎加入群聊,我们一起讨论一些更有趣的技术、商业、闲聊。


http://www.hkcw.cn/article/gRtNlWxGSu.shtml

相关文章

学习日记-day21-6.3

完成目标&#xff1a; 目录 知识点&#xff1a; 1.集合_哈希表存储过程说明 2.集合_哈希表源码查看 3.集合_哈希表无索引&哈希表有序无序详解 4.集合_TreeSet和TreeMap 5.集合_Hashtable和Vector&Vector源码分析 6.集合_Properties属性集 7.集合_集合嵌套 8.…

ABP-Book Store Application中文讲解 - Part 6: Authors: Domain Layer

ABP-Book Store Application中文讲解 - Part 6: Authors: Domain Layer 1. 汇总 ABP-Book Store Application中文讲解-汇总-CSDN博客 2. 前一章 ABP-Book Store Application中文讲解 - Part 5: Authorization-CSDN博客 项目之间的引用关系。 ​ BookAppService利用的是Cu…

智慧高铁站:数字时代交通枢纽的标杆

智慧高铁站作为现代综合交通体系的核心节点&#xff0c;通过数字技术与基础设施的深度融合&#xff0c;正在重塑旅客出行体验与车站运营模式。这一转型不仅体现在技术应用层面&#xff0c;更代表着交通服务理念的根本性变革&#xff0c;为现代交通枢纽建设树立了全新标杆。 一、…

ARM架构推理Stable Diffusiond

代码仓库&#xff1a; https://github.com/siutin/stable-diffusion-webui-docker.git Docker容器地址&#xff1a; https://hub.docker.com/r/siutin/stable-diffusion-webui-docker/tags git clone https://github.com/siutin/stable-diffusion-webui-docker.git cd stabl…

关于 KWDB 数据存储的几件事儿

邻近粽子节&#xff0c;KWDB 的朋友给我发消息&#xff0c;问我吃过红茶味的粽子没&#xff0c;作为北方人的我一般只吃蜜枣白粽&#xff0c;还没见过茶香粽子&#xff0c;顶多泡碗祁红&#xff0c;就着茶水吃粽子。 她又问道&#xff0c;两个月时间到了&#xff0c;你准备好了…

酵母杂交那些事儿(一)

酵母单杂、酵母双杂、酵母三杂&#xff0c;仅仅一个字的区别&#xff0c;你对它们了解吗&#xff1f;这些经常用到的实验&#xff0c;它们的原理你确定都搞清楚了吗&#xff1f;如果没有&#xff0c;那么今天你就来对地方了&#xff0c;因为伯远生物&#xff08;https://plant.…

sqlite3 命令行工具详细介绍

一、启动与退出 启动数据库连接 sqlite3 [database_file] # 打开/创建数据库文件&#xff08;如 test.db&#xff09; sqlite3 # 启动临时内存数据库 (:memory:) sqlite3 :memory: # 显式启动内存数据库文件不存在时自动创建不指定文件名则使用临时内…

项目开发:【悟空博客】基于SSM框架的博客平台

目录 一.导入 1.Spirng框架 2.SpirngMVC 二.项目介绍 &#xff08;一&#xff09;项目功能 &#xff08;二&#xff09;页面展示 1.注册页面 2.登录页面 3.列表页面 4.详情页面 5.编辑页面 三.准备工作 1.用户表——userinfo 2.文章表——articleinfo 3.插入数…

大话软工笔记—分离之组织和物品

一. 组织 组织在架构中既不属于“业务架构”&#xff0c;也不属于“管理架构”&#xff0c;它是由组织结构、角色、权限等要素构成。 1. 组织的概念 组织&#xff08;名词&#xff09;&#xff0c;将资源按照某个目标构建出一个有层次的集合体&#xff0c;即组织结构。 组织…

伊吖学C笔记(5、数组、表达式、考题设计)

一、数组 数组是由同一种类数据构成的集合。就好比一个班所有同学的身高&#xff0c;一个月的日平均气温&#xff0c;抽样调查的一百个数据...等等&#xff0c;都可以当作一个数组。构建数组是为了对同类的多个数据实行高效管理。 1.数组定义 格式&#xff1a;类型说明 数组…

由docker引入架构简单展开说说技术栈学习之路

想象一下&#xff0c;你开了一家线上小卖部&#xff08;单机版&#xff09;&#xff0c;突然爆单了怎么办&#xff1f;别急&#xff0c;技术架构的升级打怪之路&#xff0c;可比哆啦A梦的口袋还神奇&#xff01; 第1关&#xff1a;单枪匹马的创业初期&#xff08;单机架构&…

Dify知识库下载小程序

一、Dify配置 1.查看或创建知识库的API 二、下载程序配置 1. 安装依赖resquirements.txt ######requirements.txt##### flask2.3.3 psycopg2-binary2.9.9 requests2.31.0 python-dotenv1.0.0#####安装依赖 pip3 install -r requirements.txt -i https://pypi.tuna.tsinghua.…

Neovim - 打造一款属于自己的编辑器(一)

前言&#xff08;劝退&#xff09; neovim 是一个现代化&#xff0c;高扩展的 vim 编辑器 fork 版本&#xff0c;适合程序员打造极致高效的开发环境。 在正式开始 neovim 配置之前&#xff0c;我还是要劝退一下的。 很多人说使用 neovim 的都是变成高手&#xff0c;但我认为…

BugKu Web渗透之本地管理员

启动场景后&#xff0c;网页显示如下&#xff1a; 看起来似乎很多n。拷贝n到文件查看器&#xff0c;没有发现异常。 步骤一&#xff1a; 右键显示源码。 暂时没有发现异常。想着拷贝n到文件查看器&#xff0c;发现末尾有注释。 步骤二&#xff1a; 看见有“”&#xff0c;想…

配置cursor

介绍整体界面 上面的是功能菜单 **打开文件&#xff1a;**在打开A文件的情况下&#xff0c;再打开B文件&#xff08;再点击一次cursor&#xff0c;重新点击打开文件夹&#xff0c;选择文件B&#xff09; 打开最近文件 左侧界面 第一个是我们所有编程的文件 第二个是在项目里…

智能考核在消防员体能考核中有哪些应用?

一、训练方式的创新 个性化训练计划&#xff1a;借助智能考核系统&#xff0c;使消防员的训练更加个性化。系统依据消防员的体能、技能等数据&#xff0c;结合训练目标和历史表现&#xff0c;运用大数据分析和人工智能算法&#xff0c;为每位消防员生成专属的训练计划。如蚂蚁…

5分钟申请edu邮箱【方案本周有效】

这篇文章主要展示的是成果。如果你是第1次看见我的内容&#xff0c;具体的步骤请翻看往期的两篇作品。先看更正补全&#xff0c;再看下一个。 建议你边看边操作。 【更正补全】edu教育申请通过方案 本周 edu教育邮箱注册可行方案 #edu邮箱 伟大无需多言 我已经验证了四个了…

阿里云为何,一个邮箱绑定了两个账号

阿里云“幽灵账号”之谜&#xff1a;同一个邮箱注销后仍有两个账号&#xff1f;深度揭秘成因与终极解决方案&#xff01; 你是否曾在阿里云上使用同一个邮箱注册过多个账号&#xff0c;明明已经**“彻底”注销了其中一个**&#xff0c;却惊愕地发现系统里依然**“幽灵般”挂着…

RM-R1:基于推理任务构建奖励模型

摘要&#xff1a;奖励建模对于通过人类反馈的强化学习使大型语言模型与人类偏好对齐至关重要。为了提供准确的奖励信号&#xff0c;奖励模型&#xff08;RM&#xff09;在分配分数或判断之前应该激发深度思考并进行可解释的推理。受最近在推理密集型任务中长链推理的进展启发&a…

常见的电子元器件字母含义

元器件在电路图和 PCB 中通常以字母 数字的形式命名&#xff0c;例如 R1、C5、U3 等。其中的字母代表元件种类&#xff0c;数字用于区分多个同类元件。本文简要介绍一下常见元器件字母的含义&#xff0c;以及实物图和对应的符号图。&#xff08;注&#xff1a;本文参考图来自立…