Skip to main content

useWindowDimensions

import {useWindowDimensions} from 'react-native';

当屏幕尺寸或字体比例发生变化时,useWindowDimensions 会自动更新其所有值。你可以像这样获取应用窗口的宽度和高度:

¥useWindowDimensions automatically updates all of its values when screen size or font scale changes. You can get your application window's width and height like so:

const {height, width} = useWindowDimensions();

示例

¥Example

属性

¥Properties

fontScale

useWindowDimensions().fontScale;

当前使用的字体的比例。某些操作系统允许用户放大或缩小字体大小以提高阅读舒适度。该属性会让你知道什么是有效的。

¥The scale of the font currently used. Some operating systems allow users to scale their font sizes larger or smaller for reading comfort. This property will let you know what is in effect.


height

useWindowDimensions().height;

你的应用占据的窗口或屏幕的高度(以像素为单位)。

¥The height in pixels of the window or screen your app occupies.


scale

useWindowDimensions().scale;

你的应用运行的设备的像素比。这些值可以是:

¥The pixel ratio of the device your app is running on. The values can be:

  • 1 表示 1 点等于 1 像素(通常 PPI/DPI 为 96、某些平台上为 76)。

    ¥1 which indicates that one point equals one pixel (usually PPI/DPI of 96, 76 on some platforms).

  • 23 表示 Retina 或高 DPI 显示屏。

    ¥2 or 3 which indicates a Retina or high DPI display.


width

useWindowDimensions().width;

你的应用占用的窗口或屏幕的宽度(以像素为单位)。

¥The width in pixels of the window or screen your app occupies.