我想在视图中添加一个全屏图像,所以我写了这段代码

return (
    <View style={styles.container}>
        <Image source={require('image!egg')}  style={styles.backgroundImage}/>
    </View>
)

并将其定义为

var styles = StyleSheet.create({
container: {
     flex: 1,
     justifyContent: 'center',
     alignItems: 'center',
     backgroundColor: '#F5FCFF',
     flexDirection: 'column',
},
     backgroundImage:{
     width:320,
     height:480,
   }
...

但我应该如何找到iPhone屏幕的实际大小?

我见过一个API来访问像素密度,但没有关于屏幕大小...

推荐答案

你可以在一个<Image>元素上使用flex: 1个样式来填充整个屏幕.然后,可以使用其中一种图像大小调整模式,使图像完全填充元素:

<Image source={require('image!egg')} style={styles.backgroundImage} />

风格:

import React from 'react-native';

let { StyleSheet } = React;

let styles = StyleSheet.create({
  backgroundImage: {
    flex: 1,
    resizeMode: 'cover', // or 'stretch'
  }
});

我很确定你可以摆脱<View>个包装你的图片,这将起作用.

React-native相关问答推荐

REACT-Native-Render-html中所有文本的默认字体大小

使用 Auth Token 标头react 本机图像,如何处理 401?

react native 如何更新倒置平面列表的数组中的项目

useState 函数似乎阻止了 Animated.timing 事件?

找不到变量:firebase/react native(expo)上的 IDBIndex

找不到变量 atob

React中类似于 React Native 中的 FlatList

React Redux 使用带有连接组件的 HOC

React Native Card Carousel 视图?

react-native: 'adb' 不是内部或外部命令、可运行程序或批处理文件

xcrun:错误:SDK "iphoneos" cannot be located

eslint Parsing error: Unexpected token =

在react native 中出现Cannot read property 'pick算法rithm' of null错误

在 Android React Native 应用程序中正确使用 Intl

React-Native:FlatList onRefresh not called on pull up.

使用带有样式组件的动画

React Native - Firebase 身份验证持久性不起作用

使用 React Navigation 导航堆栈时重新渲染组件

无法运行 jetifier React Native

react-native图像预取