基础篇章:React Native之Flexbox的讲解(Height and Width)
Height and Width一个组件的高度和宽度,决定了它在屏幕上显示的大小。 固定尺寸最简单的设置组件的尺寸的方法就是通过添加一个固定的宽度和高度。所有尺寸大小在React Native没有单位的,代表着独立的像素密度。 官网例子import React,{ Component } from 'react';
import { AppRegistry,View } from 'react-native';
class FixedDimensionsBasics extends Component {
render() {
return (
<View> <View style={{width: 50,height: 50,backgroundColor: 'powderblue'}} /> <View style={{width: 100,height: 100,backgroundColor: 'skyblue'}} /> <View style={{width: 150,height: 150,backgroundColor: 'steelblue'}} /> </View> ); } }; AppRegistry.registerComponent('AwesomeProject',() => FixedDimensionsBasics);
弹性宽高我们可以在组件样式中使用flex让组件根据可用空间动态的收缩和扩展。通常情况下我们可以使用flex: 1,告诉某个组件来填充剩余的所有的空间,如果是多个组件的话,则是所有的这些组件去平分父容器中的剩余的所有空间。。如果这些并列的子组件的flex值不一样,则谁的值更大,谁占据剩余空间的比例就更大(跟我们android中weight的用法差不多)。 官网例子import React,View } from 'react-native';
class FlexDimensionsBasics extends Component {
render() {
return (
// Try removing the `flex: 1` on the parent View.
// The parent will not have dimensions,so the children can't expand.
// What if you add `height: 300` instead of `flex: 1`?
<View style={{flex: 1}}>
<View style={{flex: 1,backgroundColor: 'powderblue'}} /> <View style={{flex: 2,backgroundColor: 'skyblue'}} /> <View style={{flex: 3,() => FlexDimensionsBasics);
效果图: 关于高度和宽度就讲这些吧,其实内容都是翻译与官网的docs,地址: Flexbox一个组件可以使用Flexbox指定其子组件或元素之间的布局。Flexbox旨在为不同的屏幕上提供一致的布局。 通常情况下,我们结合使用flexDirection、alignItems和 justifyContent三个样式属性就已经能够实现我们所需的布局。
Flex Direction向一个组件的样式中添加Flex Direction可以决定一个布局的主轴。子元素应该沿着水平方向(row)排列,还是沿着竖直方向(column)排列呢?默认值是竖直(column)方向。 官网例子import React,View } from 'react-native';
class FlexDirectionBasics extends Component {
render() {
return (
// Try setting `flexDirection` to `column`.
<View style={{flex: 1,flexDirection: 'row'}}>
<View style={{width: 50,backgroundColor: 'powderblue'}} /> <View style={{width: 50,backgroundColor: 'skyblue'}} /> <View style={{width: 50,() => FlexDirectionBasics);
效果图: Justify Content向组件的样式中添加Justify Content可以决定其子元素沿着主轴的排列方式。子元素应该分布在主轴的开始端,还是中间,最后,还是均匀分布?可用的选项有:flex-start、center、flex-end、space-around以及space-between。
官网例子import React,View } from 'react-native';
class JustifyContentBasics extends Component {
render() {
return (
// Try setting `justifyContent` to `center`.
// Try setting `flexDirection` to `row`.
<View style={{
flex: 1,flexDirection: 'column',justifyContent: 'space-between',}}>
<View style={{width: 50,() => JustifyContentBasics);
效果图: Align Items向组件的样式(style)中添加alignItems可以决定其子元素沿着次轴(就是与主轴垂直的轴,比如若主轴方向为row,则次轴方向为column)的排列方式。子元素是应该靠近次轴的开始端还是中间,还是末端,亦或是拉伸来填补呢?可用选项有:flex-start、center、flex-end以及stretch。
官网例子import React,View } from 'react-native';
class AlignItemsBasics extends Component {
render() {
return (
// Try setting `alignItems` to 'flex-start'
// Try setting `justifyContent` to `flex-end`.
// Try setting `flexDirection` to `row`.
<View style={{
flex: 1,justifyContent: 'center',alignItems: 'center',() => AlignItemsBasics);
效果图: 文章翻译并参考于Flexbox官方文档,地址: 好了,到这里关于Flexbox的讲解就讲到这里了,关于Flexbox运用,上面的例子只是冰山一角,要想真正熟练掌握,还得靠自己亲自动手去写,去实践,才能够真正来理解各个属性的意思。赶紧动手去实践吧。
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |