react-native 布局篇之position

作者: 甘言川 | 来源:发表于2016-06-27 22:21 被阅读12387次

position布局

position:enum('absolute','relative')。先简单的看一下示例图

Snip20160627_1.png
  • position:'relative'
    相对布局。这个和html的position有很大的不同,他的相对布局不是相对于父容器,而是相对于兄弟节点。
  • position:'absolute'
    绝对布局。这个是相对于父容器进行据对布局。绝对布局是脱离文档流的,不过奇怪的是依旧在文档层次结构里面,这个和html的position也很大不一样。另外还有一个和html不一样的是,html中position:absolute要求父容器的position必须是absolute或者relative,如果第一层父容器position不是absolute或者relative,在html会依次向上递归查询直到找到为止,然后居于找到的父容器绝对定位。
  • position 示例代码
/**
 * Created by GXZ on 16/6/27.
 */
import React,{Component} from 'react';
import {
    Text,
    View,
    ScrollView
} from 'react-native';

export default class PositionExample extends Component {
    constructor(props) {
        super(props);
        this.state = {};
    }

    render() {
        return (
            <ScrollView>
                <View style={{flex:1}}>
                    <Text>FlexBox布局</Text>
                    <View style={styles.container}>
                        <View style={styles.box1}/>
                        <View style={[styles.box2]}/>
                        <View style={[styles.box3]}/>
                    </View>
                    <Text>position=relative,top:20</Text>
                    <View style={styles.container}>
                        <View style={styles.box1}/>
                        <View style={[styles.box2,{position:'relative',top:20}]}></View>
                        <View style={styles.box3}/>
                    </View>
                    <Text>position=absolute,top:20</Text>
                    <View style={styles.container}>
                        <View style={styles.box1}/>
                        <View style={[styles.box2,{position:'absolute',top:20}]}></View>
                        <View style={styles.box3}/>
                    </View>
                </View>
            </ScrollView>
        );
    }
}

const styles = {
    container: {
        height: 180,
        backgroundColor: '#CCCCCC',
        marginBottom: 10,
    },
    box1: {
        width: 50,
        height: 50,
        backgroundColor: '#FF0000'
    },
    box2: {
        width: 50,
        height: 50,
        backgroundColor: '#00FF00'
    },
    box3: {
        width: 50,
        height: 50,
        backgroundColor: '#0000FF'
    }
};

相关文章

网友评论

  • 49855d33d424:垃圾文章,自己先看看web端的position定义吧
  • seven_2547:怎么让你一个position:absolute定位的弹出层在左右组件的最上面显示?
  • 圐圙:android上有没有好的实现方法来实现IOS的那种标题栏,普通的方法,标题是不居中的;
    1328776ac5f0:@圐圙 flex布局就可以了。左右定长,中间Title设置flex:1

本文标题:react-native 布局篇之position

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