React-native 获取屏幕宽度/高度/像素

前端之家收集整理的这篇文章主要介绍了React-native 获取屏幕宽度/高度/像素前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
* 2811132560@qq.com
* 说明:项目名为Demo1
* 文件名:Main.js(注意引用)
*/

import React,{ Component } from ‘react’;
//注册一下,你要用什么组件
import {
AppRegistry,
StyleSheet,
PixelRatio,
Text,
View
} from ‘react-native’;

//获取
var Dimensions = require(“Dimensions”);
//var width = Dimensions.get(‘window’).width;
//var height= Dimensions.get(‘window’).height;

var {width,height} = Dimensions.get(‘window’);

var pi = PixelRatio.get();

export default class Demo1 extends Component {

render() {
    return (
        <View style={styles.container}>
            <Text>
                我的屏幕宽度:{width},{'\n'}
                我的屏幕高度:{height},{'\n'}
                我的屏幕像素:{pi}
            </Text>
    );
}

}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
backgroundColor: ‘#F5FCFF’,
flexDirection: ‘row’,
height: 100,
padding: 20
},
});

module.exports = Demo1;

猜你在找的React相关文章