freeCodeCamp/guide/chinese/react-native/screen-dimensions/index.md

26 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: Screen Dimensions
localeTitle: 屏幕尺寸
---
## React Native - 屏幕尺寸
React Native使用每英寸点数DPI来测量用户界面UI的大小和UI上显示的任何内容。这种类型的测量允许应用程序在各种屏幕尺寸和像素密度下看起来均匀。
对于标准用例可以开发应用程序而无需知道用户设备的细节例如像素密度因为UI元素将自动缩放。当需要时有`PixelRatio` API例如`PixelRatio`用于查找用户设备的像素密度。
为了获得用户设备的窗口或屏幕高度/宽度React Native有一个名为`Dimensions`的API。
```js
import { Dimensions } from 'react-native';
```
以下是`Dimensions` API提供的方法
```js
Dimensions.get('window').height;
Dimensions.get('window').width;
Dimensions.get('screen').height;
Dimensions.get('screen').width;
```
**注意过去使用Dimensions API存在一些已知问题例如在用户旋转设备时未返回正确的信息。在部署应用程序之前最好确保在实际设备上进行测试。**