React Native FlexBox布局(二) 应用篇

  • 1、获取屏幕宽高

RN通过Dimensions组件来获取设备信息,

Dimensions路径:
.../node_modules/react-native/Libraries/Utilities

获取代码:

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

const Dimensions = require('Dimensions');
const {width, height, scale} = Dimensions.get('window');

class RNHybrid extends Component {

  render() {
    return(
        <View style={{justifyContent:'center',alignItems:'center',flex:1}}>
        <Text>
               当前的屏幕的宽度是: {width + '\n'}
               当前的屏幕的高度是: {height + '\n'}
               当前的屏幕的分辨率是: {scale + '\n'}
        </Text>
        </View>
      );
  }
}

AppRegistry.registerComponent('RNHybrid', () => RNHybrid);

运行结果(6s模拟器):







  • 2、应用示例

2.1绝对定位和相对定位

  
相对定位

usage:

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

const Dimensions = require('Dimensions');
const {width, height, scale} = Dimensions.get('window');

class RNHybrid extends Component {

  render() {
    return(
        <View style={{justifyContent:'center',alignItems:'center',flex:1}}>
             <View style={{backgroundColor:'black',paddingTop:50,width:width,height:200}}>
                <Image source={{uri:'test'}} style={styles.image1Style}/>
             </View>
        </View>
      );
  }
}

const styles = StyleSheet.create({
    image1Style:{
        position:'relative',
        width:100,
        height:100,
        left:200,
        top:0,
    },
});

AppRegistry.registerComponent('RNHybrid', () => RNHybrid);

效果:




总结:相对定位遵守父类内边距设置,如: 例子内边距为50


绝对定位

usage( css  position样式修改为 position:'absolute',)。

效果:





总结:绝对定位父类内边距设置不起作用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星宇大前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值