React-native之Flexbox
本文总结:
我们学到了 React Native 的 Flexbox 布局,它让写样式变得更方便啦! Flexbox 就像一个有弹性的盒子,有主轴和交叉轴(行或列)。
在 RN 里写样式要用 StyleSheet.create 对象,属性名是驼峰命名。
文章还介绍了如何用 Platform.select 给不同平台写样式,以及使用 styled-components 这个库的简单例子(虽然我们主要还是用 StyleSheet)。
文章还通过三栏布局和横向布局的例子,展示了 flexDirection, alignItems, justifyContent 和 alignSelf 这些属性的用法。
特别喜欢 alignSelf: 'stretch' 能让元素填充空间的技巧!
最后,通过抽象出 Row 和 Col 组件,我看到了如何用 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属性是决定了主轴的方向,上到下或者左到右,而alignItem和justifyContent属性决定了元素的排列和间隔。
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
假设我们要实现这样一个效果,我们该如何实现?
在我们的意识中,整个布局有行、列。那我们同样也可以抽象出col、row组件分别代表行列。我们直接上代码吧。
可以看到我们分别确定了col、row的方向和排列。
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、总结
欢迎加入群聊,我们一起讨论一些更有趣的技术、商业、闲聊。
React-native之Flexbox的更多相关文章
- React Native之FlexBox介绍和使用
# 前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会 ...
- React Native之FlexBox布局
参考原文链接:https://www.cnblogs.com/wujy/p/5841685.html 弹性盒模型(The Flexible Box Module),又叫Flexbox,意为“弹性布局” ...
- React Native,flexbox布局
Flexbox布局 flex:使组件在可利用的空间内动态地扩张或收缩.flex:1会使组件撑满空间.当有多个组件都指定了flex的值,那么谁的flex值大谁占得空间就大,占得大小的比例就是flex值的 ...
- React Native填坑之旅--布局篇
代码在这里: https://github.com/future-challenger/petshop/tree/master/client/petshop/src/controller 回头看看RN ...
- React Native创建一个APP
React Native 结合了 Web 应用和 Native 应用的优势,可以使用 JavaScript 来开发 iOS 和 Android 原生应用.在 JavaScript 中用 React 抽 ...
- React Native布局
一款好的APP离不了一个漂亮的布局,本文章将向大家分享React Native中的布局方式FlexBox. 在React Native中布局采用的是FleBox(弹性框)进行布局. FlexBox提供 ...
- React Native FlexBox
FlexBox 是React Native布局的一种算法,目的是为了适配不同尺寸的屏幕而设计的. 使用时最关键的就是flex关键字的用法. flex用于修饰当前View在父视图中的占比. 占比如何计算 ...
- React Native 开发之 (05) flexbox布局
一 flexbox布局 1 flex布局 flexbox是ReactNative 应用开发中必不可少的内容,也是最常用的内容. 传统的页面布局是基于盒子模型,依赖定位属性,流动属性和显示属性来解决. ...
- React Native 弹性布局FlexBox
React Native采用一中全新的布局方式:FlexBox(弹性布局).可以很方便的实现各种复杂布局,是全新的针对web和移动开发布局的一种实现方式. 何为FlexBox? 完整名称为:the f ...
- React Native学习(九)—— 使用Flexbox布局
本文基于React Native 0.52 Demo上传到Git了,有需要可以看看,写了新内容会上传的.Git地址 https://github.com/gingerJY/React-Native-D ...
随机推荐
- sourcetree 重新设置git账号密码
设置提交git账号邮箱 到项目根目录,执行 vi ~/.gitconfig ,直接编辑修改即可 重新设置git登陆账号密码 打开 sourcetree 的偏好设置,选择高级,然后移除即可
- websocket: the client is not using the websocket protocol: ‘upgrade’ token not found in ‘Connection’ head,客户端没有使用websocket协议:'upgrade'令牌未在'Connection'头中找到
错误分析 websocket: the client is not using the websocket protocol: 'upgrade' token not found in 'Connec ...
- 通过 JS 修改具体标签的属性的属性值
博客地址:https://www.cnblogs.com/zylyehuo/ window.addEventListener('DOMContentLoaded', function() { var ...
- linux服务器通过X11实现图形化界面显示 1 背景描述
有些LINUX服务器出于性能和效率的考虑,通常都是没有安装图形化界面的,那么图形化程序在服务器上压根儿就跑不起来,或者无法直接显示出来,这就很尴尬了!那么如何解决这个问题呢?可以基于X11 Forwa ...
- pandas数据统一绘图风格配置
在使用pandas的时候,经常会用到Dataframe或者Series的plot方法,该方法底层实际上调的还是matplotlib.pyplot的plot方法.因此,通过对pyplot模块的绘图全局参 ...
- Qt/C++开发经验小技巧311-315
关于流媒体推拉流延时的几点说明. 经常看到一些流媒体相关的程序,号称零延迟,不用怀疑,这肯定吹牛逼的. 搞音视频开发,有个核心的指标就是实时性,也就是延迟多少毫秒,这个问题问的也是最多的. 音视频文件 ...
- 一文速通Python并行计算:04 Python多线程编程-多线程同步(上)—基于条件变量、事件和屏障
一文速通 Python 并行计算:04 Python 多线程编程-多线程同步(下)-基于条件变量.事件和屏障 摘要: 本文介绍了 Python 多线程同步的三种机制:条件变量(Condition).事 ...
- Vue3封装支持Base64导出的电子签名组件
效果图 准备工作 组件内用到elementPlus,vue-esign组件,使用前提前安装好. 组件代码 <template> <!-- 签名容器 --> <div cl ...
- postgresql的日期函数
一个to_char干完所有的活.包括日期的转换 函数 返回类型 描述 实例 to_char(timestamp, text) text 将时间戳转换为字符串 to_char(current_times ...
- Linux下more命令高级用法
我们在 Linux 环境下工作时,每天肯定会跟各种各样的文本文件打交道.这些文件,有时候会非常长,无法在一屏的空间内显示完全.所以,在查看这种文件时,我们需要分页显示.这时,我们就可以使用 more ...