什么是最好的方法来添加一个全屏幕的背景图像在反应本机

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

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 访问像素密度,但没有屏幕大小..。

知道吗?

298384 次浏览

您可以在 <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>包装你的形象,这将工作。

(现在可以使用 背景)

我就是这么做的,主要是摆脱静态的固定尺寸。

class ReactStrap extends React.Component {
render() {
return (
<Image source={require('image!background')} style={styles.container}>
... Your Content ...
</Image>
);
}
}


var styles = StyleSheet.create({
container: {
flex: 1,
// remove width and height to override fixed static size
width: null,
height: null,
}
};

注意: 此解决方案是旧的。请参考 https://facebook.github.io/react-native/docs/images.html#background-image-via-nesting

尝试这个解决方案。它是正式支持的。我刚刚测试了它,工作完美无瑕。

var styles = StyleSheet.create({
backgroundImage: {
flex: 1,
alignSelf: 'stretch',
width: null,
}
});

至于使用它作为背景图像,只需要执行以下操作。

<Image style={styles.backgroundImage}>
<View>
<Text>All your stuff</Text>
</View>
</Image>

您需要确保您的 Image 具有 resizeMode = { Image.resizeMode.include }或{ Image.resizeMode.pull } ,并将图像样式宽度设置为 null

<Image source={CharacterImage} style=\{\{width: null,}} resizeMode={Image.resizeMode.contain}/>

因为0.14这个方法不起作用,所以我构建了一个静态组件,它将使这个方法对你们来说变得简单。您可以直接粘贴它,或者将它作为组件引用。

这应该是可重用的,它将允许您添加额外的样式和属性,如同它是一个标准的 <Image />组件

const BackgroundImage = ({ source, children, style, ...props }) => {
return (
<Image
source={source}
style=\{\{flex: 1, width: null, height: null, ...style}}
{...props}>
{children}
</Image>
);
}

只要粘贴这个,然后你可以像使用图片一样使用它,它应该适合它所在的视图的整个大小(因此,如果它是顶部视图,它将填充你的屏幕。

<BackgroundImage source={require('../../assets/backgrounds/palms.jpg')}>
<Scene styles={styles} {...store} />
</BackgroundImage>

点击这里查看预览图像

我尝试了其中的几个答案,但是对于使用 response-national version = 0.19.0的 android 来说没有用。

由于某种原因,样式表中的 resizeMode 不能正常工作

backgroundImage: {
flex: 1,
width: null,
height: null,
}

在 Image 标记中,我指定了 resizeMode:

<Image source={require('path/to/image.png')} style= {styles.backgroundImage} resizeMode={Image.resizeMode.sretch}>

正如上面提到的,您可以使用 Image.resizeMode.cover 或者 include。

希望这个能帮上忙!

基于 Braden Rockwell Napier回答,我制作了这个 BackgroundImage组件

背景图片

import React, { Component } from 'react'
import { Image } from 'react-native'


class BackgroundImage extends Component {
render() {
const {source, children, style, ...props} = this.props
return (
<Image source={ source }
style={ { flex: 1, width: null, height: null, ...style } }
{...props}>
{ children }
</Image>
)
}
}
BackgroundImage.propTypes = {
source: React.PropTypes.object,
children: React.PropTypes.object,
style: React.PropTypes.object
}
export default BackgroundImage

在我的应用程序中的某个地方

 import BackgroundImage from './backgroundImage'
....
<BackgroundImage source={ { uri: "https://facebook.github.io/react-native/img/header_logo.png" } }>
<Text>Test</Text>
</BackgroundImage>

宽度和高度值为 null 对我来说不起作用,然后我想使用顶部、底部、左边和右边的位置,它起作用了。 例如:

bg: {
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
resizeMode: 'stretch',
},

还有 JSX:

<Image style={styles.bg} source=\{\{uri: 'IMAGE URI'}} />

如果你还没有解决它,反应本机 v.0.42.0有 resizeMode

<Image style=\{\{resizeMode: 'contain'}} source={require('..img.png')} />
import React, { Component } from 'react';
import { Image, StyleSheet } from 'react-native';


export default class App extends Component {
render() {
return (
<Image source=\{\{uri: 'http://i.imgur.com/IGlBYaC.jpg'}} style={s.backgroundImage} />
);
}
}


const s = StyleSheet.create({
backgroundImage: {
flex: 1,
width: null,
height: null,
}
});

你可以试试: https://sketch.expo.io/B1EAShDie(from: Github.com/dorian/sketch-reactive-native-apps)

医生: https://facebook.github.io/react-native/docs/images.html#background-image-via-nesting

如果要将其用作背景图像,则需要在 v0.46中使用新的 <ImageBackground>组件 于2017年6月底推出。它支持嵌套,而 <Image>很快就不支持了。

以下是 承诺的摘要:

我们正在删除对组件内嵌套视图的支持。我们决定这样做是因为有了这个特性就可以提供支持 <Image>intrinsinc content size是不可能的; 因此当 过渡过程完成后,就不需要指定图像了 大小显式,它可以从实际的图像位图推断。

这就是第0步。

是非常简单的插入式替换 这个功能通过非常简单的样式。请使用 而不是如果你想把东西 在里面。

您还可以将图像用作容器:

render() {
return (
<Image
source={require('./images/background.png')}
style={styles.container}>
<Text>
This text will be on top of the image
</Text>
</Image>
);
}




const styles = StyleSheet.create({
container: {
flex: 1,
width: undefined,
height: undefined,
justifyContent: 'center',
alignItems: 'center',
},
});

还有一个简单的解决办法:

<Image source={require('../assets/background.png')}
style=\{\{position: 'absolute', zIndex: -1}}/>


<View style=\{\{flex: 1, position: 'absolute'}}>


{/*rest of your content*/}
</View>

我听说过必须使用 BackoundImage,因为将来您不能嵌套 Image 标记。但是我无法让 Background Image 正确地显示我的背景。我所做的就是将我的 Image 嵌套在一个 View 标记中,同时设置外部 View 和图像的样式。键将 width 设置为 null,并将 resizeMode 设置为“ pull”。下面是我的代码:

import React, {Component} from 'react';
import { View, Text, StyleSheet, Image} from 'react-native';


export default class BasicImage extends Component {
constructor(props) {
super(props);


this.state = {};
}


render() {
return (
<View style={styles.container}>
<Image
source={this.props.source}
style={styles.backgroundImage}
/>
</View>
)
}
}


const styles = StyleSheet.create({
container: {
flex: 1,
width: null,
height: null,
marginBottom: 50
},
text: {
marginLeft: 5,
marginTop: 22,
fontFamily: 'fontawesome',
color: 'black',
fontSize: 25,
backgroundColor: 'rgba(0,0,0,0)',
},
backgroundImage: {
flex: 1,
width: null,
height: null,
resizeMode: 'stretch',
}
});

正如 Antoine 129已经说过的那样,使用 <ImageBackground>。现在不推荐对儿童使用 <Image>

class AwesomeClass extends React.Component {
render() {
return (
<ImageBackground source={require('image!background')} style={styles.container}>
<YourAwesomeComponent/>
</ImageBackground>
);
}
}


var styles = StyleSheet.create({
container: {
flex: 1,
}
};

最近到10月17日 (RN > = .46)

import React from 'react';
import {
...
ImageBackground,
} from 'react-native';


render() {
return (
<ImageBackground source={require('path/to/img')} style={styles.urStyle}>
</ImageBackground>
);
}

Http://facebook.github.io/react-native/releases/0.49/docs/images.html#background-image-via-nesting

(登记册编号 > = .46)

组件不能包含子组件,如果要在图像上方呈现内容,请考虑使用绝对定位。

或者你可以使用 < em > 图像背景 >

import React from 'react';
import {
...
StyleSheet,
ImageBackground,
} from 'react-native';


render() {
return (
    

<ImageBackground source={require('path/to/img')} style={styles.backgroundImage} >
<View style=\{\{flex: 1, backgroundColor: 'transparent'}} />
<View style=\{\{flex: 3,backgroundColor: 'transparent'}} >
</ImageBackground>
    

);
}


const styles = StyleSheet.create({
backgroundImage: {
flex: 1,
width: null,
height: null,
resizeMode: 'cover'
},
});

哦,上帝,最后我找到了一个伟大的方式反应-本土 V0.52-RC 和本土基地:

你的内容标签应该是这样的: //==============================================================

<Content contentContainerStyle={styles.container}>
<ImageBackground
source={require('./../assets/img/back.jpg')}
style={styles.backgroundImage}>
<Text>
Some text here ...
</Text>
</ImageBackground>
</Content>

你的基本风格是: //==============================================================

container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
backgroundImage:{
flex : 1,
width : '100%'
}

很管用,好朋友,玩得开心

不推荐使用 Image 更新2018年3月

  <ImageBackground
source=\{\{uri: 'https://images.pexels.com/photos/89432/pexels-photo-89432.jpeg?h=350&dpr=2&auto=compress&cs=tinysrgb'}}
style=\{\{ flex: 1,
width: null,
height: null,
}}
>
<View style=\{\{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Your Contents</Text>
</View>


</ImageBackground >

实现背景最简单的方法:

<ImageBackground style={styles.container} source={require('../../images/screen_login.jpg')}>
<View style={styles.logoContainer}>
<Image style={styles.logo}
source={require('../../images/logo.png')}
/>
</View>
<View style={styles.containerTextInput}>
< LoginForm />
</View>
</ImageBackground>


const styles = StyleSheet.create({
container: {
flex: 1,
//   backgroundColor:"#0984e3"
},
containerTextInput: {
marginTop: 10,
justifyContent: 'center'
},


logoContainer: {
marginTop: 100,
justifyContent: 'center',
alignItems: 'center'
},
logo: {
height: 150,
width: 150
}
});

更新到图像背景

因为使用 <Image />作为容器已经被废弃了一段时间,所有的答案实际上都遗漏了一些重要的东西。为了正确使用,选择 <ImageBackground />style 还有 imageStyle道具。将所有与图像相关的样式应用于 imageStyle

例如:

<ImageBackground
source={yourImage}
style=\{\{
backgroundColor: '#fc0',
width: '100%', // applied to Image
height: '100%'
}}
imageStyle=\{\{
resizeMode: 'contain' // works only here!
}}
>
<Text>Some Content</Text>
</ImageBackground>

Https://github.com/facebook/react-native/blob/master/libraries/image/imagebackground.js

import { ImageBackground } from "react-native";
<ImageBackground
style=\{\{width: '100%', height: '100%'}}
source={require('../assets/backgroundLogin.jpg ')}> //path here inside
<Text>React</Text>
</ImageBackground>

我用这段代码解决了我的背景图像问题。

import React from 'react';
import { StyleSheet, Text, View,Alert,ImageBackground } from 'react-native';


import { TextInput,Button,IconButton,Colors,Avatar } from 'react-native-paper';


class SignInScreen extends React.Component {


state = {
UsernameOrEmail  : '',
Password : '',
}
render() {
return (
<ImageBackground  source={require('../assets/icons/background3.jpg')} style {styles.backgroundImage}>
<Text>React Native App</Text>
</ImageBackground>
);
}
}




export default SignInScreen;


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

图像背景可能有限制

实际上,您可以直接使用它,而且它不会被弃用。

如果你想在“反应本机”中添加背景图像,同时也想在背景图像中添加其他元素,请按照下面的步骤操作:

  1. 创建容器视图
  2. 创建一个100% 宽度和高度的 Image 元素
  3. 在 Image 元素下面创建另一个 View 元素,其位置为:

这是我使用的代码:

import React, { Component } from 'react';
import {Text, View, Image} from 'react-native';
import Screen from '../library/ScreenSize'


export default class MenuScreen extends Component {
static navigationOptions = {
header: null
}
render() {
return (
<View style=\{\{ flex: 1 }}>
<Image
style=\{\{
resizeMode: "cover",
width: "100%",
height: "100%",
justifyContent: "center",
alignItems: "center",
opacity: 0.4
}}
source={require("../assets/images/menuBackgroundImage.jpg")}
></Image>


<View style=\{\{
width: Screen.width,
height: Screen.height * 0.55,
position: 'absolute',
bottom: 0}}>
<Text style=\{\{
fontSize: 48
}}>Glad to Meet You!</Text>
</View>
</View>
);
}
}


享受编码... 。

产出:

This is the output of my code.

为了处理这个用例,您可以使用与 <Image>具有相同道具的 <ImageBackground>组件,并在其上添加您想要添加的任何子级。

例如:

return (
<ImageBackground source={...} style=\{\{width: '100%', height: '100%'}}>
<Text>Inside</Text>
</ImageBackground>
);

更多信息: 本地反应

请注意,您必须指定某些宽度和高度样式属性。

对于我来说,这个工作很好,我使用了图像背景来设置背景图片:

import React from 'react';
import { SafeAreaView, StyleSheet, ScrollView, View, Text, StatusBar, Image, ImageBackground } from 'react-native';
const App: () => React$Node = () => {
return (
<>
<StatusBar barStyle="dark-content" />
<View style={styles.container}>
<ImageBackground source={require('./Assets.xcassets/AppBackGround.imageset/2x.png')} style={styles.backgroundImage}>
<View style={styles.sectionContainer}>
<Text style={styles.title}>Marketing at the speed of today</Text>
</View>
</ImageBackground>
</View>
</>
);
};




const styles = StyleSheet.create({
container: {
flex: 1,
fontFamily: "-apple-system, BlinkMacSystemFont Segoe UI",
justifyContent: "center",
alignItems: "center",
},
backgroundImage: {
flex: 1,
resizeMode: 'cover',
height: '100%',
width: '100%'
},
title:{
color: "#9A9A9A",
fontSize: 24,
justifyContent: "center",
alignItems: "center",
},
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
},
});


export default App;

如果你想添加一个背景图片,你可以使用,但首先你必须导入这个从’反应本机’如下:

import {ImageBackground} from 'react-native';

然后:

    export default function App() {
    

return (
<View style={styles.body}>


<ImageBackground source={require('./path/to/yourimage')} style={styles.backgroungImage}>
<View style={styles.container}>Hello world!
</View>
</ImageBackground>
</View>
);
}


const styles = StyleSheet.create({
backgroungImage: {
flex: 1,
maxWidth: '100%',
}
});

import React from 'react';
import {
...
StyleSheet,
ImageBackground,
} from 'react-native';


render() {
return (
    

<ImageBackground source={require('path/to/img')} style={styles.backgroundImage} >
<View style=\{\{flex: 1, backgroundColor: 'transparent'}} />
<View style=\{\{flex: 3,backgroundColor: 'transparent'}} >
</ImageBackground>
    

);
}


const styles = StyleSheet.create({
backgroundImage: {
flex: 1,
width: null,
height: null,
resizeMode: 'cover'
},
});