当前位置: 代码迷 >> 综合 >> react native 获取当前屏幕的宽度,高度,当前运行的机型
  详细解决方案

react native 获取当前屏幕的宽度,高度,当前运行的机型

热度:97   发布时间:2024-01-26 22:01:21.0

当前屏幕的宽度:Dimensions.get('window').width

当前屏幕的高度:Dimensions.get('window').height

当前屏幕的分辨率:Dimensions.get('window').scale

当前运行的平台:Platform.OS

 

import React from 'react';
import {SafeAreaView,StyleSheet,ScrollView,View,Text,StatusBar,Dimensions,Platform,
} from 'react-native';import {Colors} from 'react-native/Libraries/NewAppScreen';const App: () => React$Node = () => {return (<><StatusBar barStyle="dark-content" /><SafeAreaView><ScrollViewcontentInsetAdjustmentBehavior="automatic"style={styles.scrollView}><View><Text>当前屏幕的宽度:{Dimensions.get('window').width + '\n'}</Text><Text>当前屏幕的高度:{Dimensions.get('window').height + '\n'}</Text><Text>当前屏幕的分辨率:{Dimensions.get('window').scale + '\n'}</Text><Text>当前运行的平台:{Platform.OS}</Text></View></ScrollView></SafeAreaView></>);
};
// 样式表const styles = StyleSheet.create({scrollView: {backgroundColor: Colors.lighter,},
});export default App;

 

  相关解决方案