我正在使用 react native 0.63.4 并且我<View style={{flex: 1}}>用于每个页面的根元素。
我Dimensions.get("screen").width用来使元素具有响应性。由于某种原因,屏幕宽度的值不正确,而是一个更大的值。因此,屏幕上的每个元素都会变大。
我已经用许多设备进行了测试。我只在 Galaxy S20 Ultra 和 S10 Lite 上看到这个问题。安装 Android 11 和 OneUi 3.0 更新后出现此问题。
代码:
const {height: screen_height, width: screen_width} = Dimensions.get('screen');
<View style={{ flex: 1,justifyContent:"center",alignItems:"center" }}>
<Text style={{fontSize:screen_width/15}}>text</Text>
</View>;
Run Code Online (Sandbox Code Playgroud)
什么可能导致问题?
更新:仅当设备的屏幕分辨率设置为 3200x1440 时才会出现此问题。该应用程序在较低分辨率下按预期工作。
替换这段代码:
const {height: screen_height, width: screen_width} = Dimensions.get('screen');
Run Code Online (Sandbox Code Playgroud)
有了这个:
let scale = Dimensions.get('screen').scale / Dimensions.get('window').scale;
const screen_height = Dimensions.get('window').height * scale;
const screen_width = Dimensions.get('window').width * scale;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
443 次 |
| 最近记录: |