React Native 之判断横竖屏显示

1.这应用启动时能检测到设备是横置还是竖直的

2.当设备横竖屏切换时候 能坚持到这个事件

检测设备是竖直还是横置的一个方法获取当前设备屏幕的宽与高,正常的设备在竖着的时候,宽小于高;而横置时,宽大与高。


/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React,{ Component } from 'react';
import {
  AppRegistry,StyleSheet,Text,View
} from 'react-native';
let Dimensions = require('Dimensions');

export default class ViewProject extends Component {

  render() {
    return (
      <View style={styles.container}
        onLayout={this._onLayout}
      >
       <Text style={styles.welcome}
         onLayout={this._onLayoutText}
        >
         Welcome to React Native
       </Text>
      </View>
    );
  }

  _onLayout(event){
  {
    let {x,y,width,height} = event.nativeEvent.layout;
    console.log("width from View onLayout:"+width);
    console.log("height from View onLayout:"+height);
    console.log("X from View onLayout:"+x);
    console.log("y from View onLayout:"+y);
    if(height>width){
     console.log("通过View判断 竖屏");
     }else{
     console.log("通过View判断 横屏");
    }
  }
  let {width,height} = Dimensions.get('window');
  console.log("width from Dimensions:"+width);
  console.log("height from Dimensions:"+height);
  
  if(height>width){
   console.log("Dimensions 竖屏");
  }else{
   console.log("Dimensions 横屏");
  }
  
  console.log("\r\n");
 
  
}

_onLayoutText(event){
  let {x,height} = event.nativeEvent.layout;
  console.log("width from Text onLayout:"+width);
  console.log("height from Text onLayout:"+height);
  console.log("X from Text onLayout:"+x);
  console.log("y from Text onLayout:"+y);
  console.log("\r\n");
 }

}

const styles = StyleSheet.create({
  container: {
    flex: 1,justifyContent:"center",alignItems:"center",backgroundColor:"#F5FCFF",},welcome:{
    fontSize:20,textAlign:'center',margin:10,}
});
AppRegistry.registerComponent('ViewProject',() => ViewProject);

相关文章

一、前言 在组件方面react和Vue一样的,核心思想玩的就是组件...
前言: 前段时间学习完react后,刚好就接到公司一个react项目...
前言: 最近收到组长通知我们项目组后面新开的项目准备统一技...
react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...