React Native - Layout&Flexbox

React Native - Layout&Flexbox 首页 / React Native入门教程 / React Native - Layout&Flexbox

React Native Flexbox是一种算法,用于指定组件子代的布局。它在不同的屏幕尺寸上提供了一致的布局。

Flexbox属性

Flexbox提供了三个主要属性来实现所需的布局。这些属性是:flexDirection,justifyContent和alignItems。

PropertyValuesDescription
flexDirection'column', 'row'用于垂直或水平对齐其元素。
justifyContent'center', 'flex-start', 'flex-end', 'space-around', 'space-between'用于在容器内分配元素。
alignItems'center', 'flex-start', 'flex-end', 'stretched'用于沿辅助轴在容器内部分配元素(与flexDirection相反)。

Flexbox方向

flexDirection在其布局的主轴上将样式添加到组件。它具有一个属性行和列,分别用于水平和垂直地组织子级。默认的flexDirection是一列。

import React, { Component } from 'react';
import { StyleSheet,View } from 'react-native';

export default class FlexDirectionBasics extends Component {
    render() {
        return (
            <View style={styles.container}>
                <View style={styles.powderblue} />
                <View style={styles.skyblue} />
                <View style={styles.steelblue} />
            </View>
        );
    }
}
const styles = StyleSheet.create({
    container:{
        flex: 1,
        flexDirection: 'row',// 水平设置元素,列。
    },
    powderblue:{
        width: 60,
        height: 60,
        backgroundColor: 'powderblue',
    },
    skyblue:{
        width: 60,
        height: 60,
        backgroundColor: 'skyblue',
    },
    steelblue:{
        width: 60,
        height: 60,
        backgroundColor: 'steelblue',
    }
})

输出

React Native Layout and Flexbox

Flexbox分布

justifyContent确定子组件沿主轴的分布。子组件在起点(start),终点(end(,中心(center)或空间均匀分布。

import React, { Component } from 'react';
import { StyleSheet,View } from 'react-native';

export default class JustifyContentBasics extends Component {
    render() {
        return (
            <View style={styles.container}>
                <View style={styles.powderblue} />
                <View style={styles.skyblue} />
                <View style={styles.steelblue} />
            </View>
        );
    }
}
const styles = StyleSheet.create({
    container:{
        flex: 1,
        flexDirection: 'column',//水平设置元素`。
        justifyContent: 'center',

    },
    powderblue:{
        width: 60,
        height: 60,
        backgroundColor: 'powderblue'
    },
    skyblue:{
        width: 60,
        height: 60,
        backgroundColor: 'skyblue',
    },
    steelblue:{
        width: 60,
        height: 60,
        backgroundColor: 'steelblue',
    }
})

输出

React Native Layout and Flexbox

Flexbox对齐

alignItems确定子组件沿辅助轴的对齐方式。如果主轴是列(column),则辅助是行(row),而主轴是行(row),则辅助是列(column)。使用alignItems可以使子项在开始(start),结束(end),居中(center)或拉伸(stretched)时对齐。

import React, { Component } from 'react';
import { StyleSheet,View } from 'react-native';

export default class AlignItemsBasics extends Component {
    render() {
        return (
            <View style={styles.container}>
                <View style={styles.powderblue} />
                <View style={styles.skyblue} />
                <View style={styles.steelblue} />
            </View>
        );
    }
}
const styles = StyleSheet.create({
    container:{
        flex: 1,
        flexDirection: 'column',//水平设置元素`。
        justifyContent: 'center',
        alignItems: 'stretch',
    },
    powderblue:{
        width: 60,
        height: 60,
        backgroundColor: 'powderblue'
    },
    skyblue:{
        width: 60,
        height: 60,
        backgroundColor: 'skyblue',
    },
    steelblue:{
        /*width: 60,*/
        height: 60,
        backgroundColor: 'steelblue',
    }
})

输出

React Native Layout and Flexbox

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

持续交付36讲 -〔王潇俊〕

重学前端 -〔程劭非(winter)〕

玩转Spring全家桶 -〔丁雪丰〕

容器实战高手课 -〔李程远〕

体验设计案例课 -〔炒炒〕

打造爆款短视频 -〔周维〕

现代React Web开发实战 -〔宋一玮〕

云原生架构与GitOps实战 -〔王炜〕

结构执行力 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)