美文网首页
React Native中的Flexbox布局

React Native中的Flexbox布局

作者: 石小泉 | 来源:发表于2017-01-13 13:44 被阅读13次

    简介

    我们在React Native中使用flexbox规则来指定某个组件的子元素的布局。Flexbox可以在不同屏幕尺寸上提供一致的布局结构。相对于Native开发的布局更加快捷方便。

    Flexbox使用flexDirection、alignItems和 justifyContent三个样式属性就已经能满足大多数布局需求

    flexDirection

    flexDirection可以决定zu jian组件布局的主轴,子元素会沿着主轴排列,或水平或垂直。

    flexDirection的默认值是竖直轴(column)方向

    column(默认)

    export default class AsomeProject extends Component {
    
        constructor(props) {
            super(props);
        }
    
        render() {
            return (
                <View style={{flex: 1}}>
                    <View style={{width: 50, height: 50, backgroundColor: 'red'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'green'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'blue'}} />
                </View>
            );
        }
    }
    
    AppRegistry.registerComponent('AsomeProject', () => AsomeProject);
    
    这里写图片描述

    column-reverse

    export default class AsomeProject extends Component {
    
        constructor(props) {
            super(props);
        }
    
        render() {
            return (
                <View style={{flex: 1, flexDirection: 'column-reverse'}}>
                    <View style={{width: 50, height: 50, backgroundColor: 'red'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'green'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'blue'}} />
                </View>
            );
        }
    }
    
    AppRegistry.registerComponent('AsomeProject', () => AsomeProject);
    
    这里写图片描述

    row

    export default class AsomeProject extends Component {
    
        constructor(props) {
            super(props);
        }
    
        render() {
            return (
                <View style={{flex: 1, flexDirection: 'row'}}>
                    <View style={{width: 50, height: 50, backgroundColor: 'red'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'green'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'blue'}} />
                </View>
            );
        }
    }
    
    AppRegistry.registerComponent('AsomeProject', () => AsomeProject);
    
    
    这里写图片描述

    row-reverse

    export default class AsomeProject extends Component {
    
        constructor(props) {
            super(props);
        }
    
        render() {
            return (
                <View style={{flex: 1, flexDirection: 'row-reverse'}}>
                    <View style={{width: 50, height: 50, backgroundColor: 'red'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'green'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'blue'}} />
                </View>
            );
        }
    }
    
    AppRegistry.registerComponent('AsomeProject', () => AsomeProject);
    
    
    这里写图片描述

    justifyContent

    在组件的style中指定justifyContent可以决定其子元素沿着主轴的排列方式。子元素是应该靠近主轴的起始端还是末尾段分布呢?亦或应该均匀分布?对应的这些可选项有:flex-start、center、flex-end、space-around以及space-between。

    flex-start

    export default class AsomeProject extends Component {
    
        constructor(props) {
            super(props);
        }
    
        render() {
            return (
                <View style={{ flex: 1, flexDirection: 'column',justifyContent: 'flex-start'}}>
                    <View style={{width: 50, height: 50, backgroundColor: 'red'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'green'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'blue'}} />
                </View>
            );
        }
    }
    
    AppRegistry.registerComponent('AsomeProject', () => AsomeProject);
    
    这里写图片描述

    center

    export default class AsomeProject extends Component {
    
        constructor(props) {
            super(props);
        }
    
        render() {
            return (
                <View style={{ flex: 1, flexDirection: 'column',justifyContent: 'center'}}>
                    <View style={{width: 50, height: 50, backgroundColor: 'red'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'green'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'blue'}} />
                </View>
            );
        }
    }
    
    AppRegistry.registerComponent('AsomeProject', () => AsomeProject);
    
    这里写图片描述

    flex-end

    export default class AsomeProject extends Component {
    
        constructor(props) {
            super(props);
        }
    
        render() {
            return (
                <View style={{ flex: 1, flexDirection: 'column',justifyContent: 'flex-end'}}>
                    <View style={{width: 50, height: 50, backgroundColor: 'red'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'green'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'blue'}} />
                </View>
            );
        }
    }
    
    AppRegistry.registerComponent('AsomeProject', () => AsomeProject);
    
    这里写图片描述

    space-around

    export default class AsomeProject extends Component {
    
        constructor(props) {
            super(props);
        }
    
        render() {
            return (
                <View style={{ flex: 1, flexDirection: 'column',justifyContent: 'space-around'}}>
                    <View style={{width: 50, height: 50, backgroundColor: 'red'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'green'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'blue'}} />
                </View>
            );
        }
    }
    
    AppRegistry.registerComponent('AsomeProject', () => AsomeProject);
    
    这里写图片描述

    space-between

    export default class AsomeProject extends Component {
    
        constructor(props) {
            super(props);
        }
    
        render() {
            return (
                <View style={{ flex: 1, flexDirection: 'column',justifyContent: 'space-between'}}>
                    <View style={{width: 50, height: 50, backgroundColor: 'red'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'green'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'blue'}} />
                </View>
            );
        }
    }
    
    AppRegistry.registerComponent('AsomeProject', () => AsomeProject);
    
    这里写图片描述

    alignItems

    在组件的style中指定alignItems可以决定其子元素沿着次轴(与主轴垂直的轴,比如若主轴方向为row,则次轴方向为column)的排列方式。子元素是应该靠近次轴的起始端还是末尾段分布呢?亦或应该均匀分布?对应的这些可选项有:flex-start、center、flex-end以及stretch。

    flex-start

    export default class AsomeProject extends Component {
    
        constructor(props) {
            super(props);
        }
    
        render() {
            return (
                <View style={{ flex: 1, flexDirection: 'column', justifyContent: 'center', alignItems: 'flex-start'}}>
                    <View style={{width: 50, height: 50, backgroundColor: 'red'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'green'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'blue'}} />
                </View>
            );
        }
    }
    
    AppRegistry.registerComponent('AsomeProject', () => AsomeProject);
    
    这里写图片描述

    center

    export default class AsomeProject extends Component {
    
        constructor(props) {
            super(props);
        }
    
        render() {
            return (
                <View style={{ flex: 1, flexDirection: 'column', justifyContent: 'center', alignItems: 'center'}}>
                    <View style={{width: 50, height: 50, backgroundColor: 'red'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'green'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'blue'}} />
                </View>
            );
        }
    }
    
    AppRegistry.registerComponent('AsomeProject', () => AsomeProject);
    
    这里写图片描述

    flex-end

    export default class AsomeProject extends Component {
    
        constructor(props) {
            super(props);
        }
    
        render() {
            return (
                <View style={{ flex: 1, flexDirection: 'column', justifyContent: 'center', alignItems: 'flex-end'}}>
                    <View style={{width: 50, height: 50, backgroundColor: 'red'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'green'}} />
                    <View style={{width: 50, height: 50, backgroundColor: 'blue'}} />
                </View>
            );
        }
    }
    
    AppRegistry.registerComponent('AsomeProject', () => AsomeProject);
    
    这里写图片描述

    stretch

    注意:

    要使stretch选项生效的话,子元素在次轴方向上不能有固定的尺寸。以下面的代码为例:只有将子元素样式中的width: 50去掉之后,alignItems: 'stretch'才能生效。

    export default class AsomeProject extends Component {
    
        constructor(props) {
            super(props);
        }
    
        render() {
            return (
                <View style={{ flex: 1, flexDirection: 'column', justifyContent: 'center', alignItems: 'stretch'}}>
                    <View style={{height: 50, backgroundColor: 'red'}} />
                    <View style={{height: 150, backgroundColor: 'green'}} />
                    <View style={{height: 100, backgroundColor: 'blue'}} />
                </View>
            );
        }
    }
    
    AppRegistry.registerComponent('AsomeProject', () => AsomeProject);
    
    这里写图片描述

    相关文章

      网友评论

          本文标题:React Native中的Flexbox布局

          本文链接:https://www.haomeiwen.com/subject/ctcsbttx.html