react-native 布局篇之position

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'
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值