添加全屏背景图像的最佳方式是什么
What's the best way to add a full screen background image
我想在视图中添加一个全屏图像,所以我写了这段代码
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>
包裹您的图像,这会奏效。
(已弃用,现在您可以使用 ImageBackground)
我就是这样做的。最主要的是摆脱静态固定尺寸。
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>
您需要确保您的图像具有 resizeMode={Image.resizeMode.contain} 或 {Image.resizeMode.stretch} 并将图像样式宽度设置为 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>
我使用 react-native version = 0.19.0 尝试了其中的几个答案,但对 android 无济于事。
出于某种原因,我的样式表中的 resizeMode 无法正常工作?然而,当 sytlesheet 有
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 或包含。
希望对您有所帮助!
基于Braden Rockwell Napier's ,我做了这个BackgroundImage
组件
BackgroundImage.js
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
someWhereInMyApp.js
import BackgroundImage from './backgroundImage'
....
<BackgroundImage source={ { uri: "https://facebook.github.io/react-native/img/header_logo.png" } }>
<Text>Test</Text>
</BackgroundImage>
width 和height 值为null 对我不起作用,然后我想使用顶部、底部、左侧和右侧位置,它起作用了。
示例:
bg: {
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
resizeMode: 'stretch',
},
还有 JSX:
<Image style={styles.bg} source={{uri: 'IMAGE URI'}} />
如果你还没有解决,React Native 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>
组件 introduced at the end of June 2017。它支持嵌套,而 <Image>
很快就不会了。
这里是commit摘要:
We are removing support of nesting views inside component. We decided to do this because having this feature makes supporting
intrinsinc content size
of the <Image>
impossible; so when the
transition process is complete, there will be no need to specify image
size explicitly, it can be inferred from actual image bitmap.
And this is the step #0.
is very simple drop-in replacement which implements
this functionality via very simple styling. Please, use
instead of if you want to put something
inside.
您还可以将图像用作容器:
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>
我听说必须使用 BackgroundImage,因为将来您应该无法嵌套 Image 标签。但是我无法让 BackgroudImage 正确显示我的背景。我所做的是将我的图像嵌套在 View 标签中,并为外部 View 和图像设置样式。键将宽度设置为 null,并将 resizeMode 设置为 'stretch'。下面是我的代码:
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',
}
});
使用 <ImageBackground>
正如 antoine129 所说。现在已弃用 <Image>
和 children。
class AwesomeClass extends React.Component {
render() {
return (
<ImageBackground source={require('image!background')} style={styles.container}>
<YourAwesomeComponent/>
</ImageBackground>
);
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
}
};
最新截至 17 年 10 月 (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
(RN >= .46)
the component cannot contain children if you want to render content on top of the image, consider using absolute positioning.
或者您可以使用 ImageBackground
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'
},
});
天哪,我终于找到了 React-Native V 0.52-RC 和 native-base 的好方法:
您的内容标签应该是这样的:
//================================================ ==============
<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%'
}
效果很好朋友...玩得开心
2018 年 3 月更新不推荐使用图像使用 ImageBackground
<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
}
});
更新为 ImageBackground
由于暂时不推荐使用 <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'
}
});
ImageBackground 可能有限制
Actually, you can use directly and it is not deprecated.
如果您想在 React Native 中添加背景图片,并且还想在该背景图片上添加其他元素,请按照以下步骤操作:
- Create a Container View
- Create an Image element with 100% width and height. Also resizeMode: 'Cover'
- Create another View element under Image element with position: 'absolute'
这是我使用的代码:
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>
);
}
}
享受编码....
输出:
要处理此用例,您可以使用 <ImageBackground>
组件,它具有与 <Image>
相同的属性,并向其添加您想要在其上分层的任何子项。
示例:
return (
<ImageBackground source={...} style={{width: '100%', height: '100%'}}>
<Text>Inside</Text>
</ImageBackground>
);
更多:ImageBackground | React Native
Note that you must specify some width and height style attributes.
对我来说这很好用,我使用 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;
如果你想添加背景图片,你可以使用,但首先你必须从 'react-native' 导入它,如下所示:
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'
},
});
我想在视图中添加一个全屏图像,所以我写了这段代码
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>
包裹您的图像,这会奏效。
(已弃用,现在您可以使用 ImageBackground)
我就是这样做的。最主要的是摆脱静态固定尺寸。
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>
您需要确保您的图像具有 resizeMode={Image.resizeMode.contain} 或 {Image.resizeMode.stretch} 并将图像样式宽度设置为 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>
我使用 react-native version = 0.19.0 尝试了其中的几个答案,但对 android 无济于事。
出于某种原因,我的样式表中的 resizeMode 无法正常工作?然而,当 sytlesheet 有
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 或包含。
希望对您有所帮助!
基于Braden Rockwell Napier's BackgroundImage
组件
BackgroundImage.js
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
someWhereInMyApp.js
import BackgroundImage from './backgroundImage'
....
<BackgroundImage source={ { uri: "https://facebook.github.io/react-native/img/header_logo.png" } }>
<Text>Test</Text>
</BackgroundImage>
width 和height 值为null 对我不起作用,然后我想使用顶部、底部、左侧和右侧位置,它起作用了。 示例:
bg: {
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
resizeMode: 'stretch',
},
还有 JSX:
<Image style={styles.bg} source={{uri: 'IMAGE URI'}} />
如果你还没有解决,React Native 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>
组件 introduced at the end of June 2017。它支持嵌套,而 <Image>
很快就不会了。
这里是commit摘要:
We are removing support of nesting views inside component. We decided to do this because having this feature makes supporting
intrinsinc content size
of the<Image>
impossible; so when the transition process is complete, there will be no need to specify image size explicitly, it can be inferred from actual image bitmap.And this is the step #0.
is very simple drop-in replacement which implements this functionality via very simple styling. Please, use instead of if you want to put something inside.
您还可以将图像用作容器:
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>
我听说必须使用 BackgroundImage,因为将来您应该无法嵌套 Image 标签。但是我无法让 BackgroudImage 正确显示我的背景。我所做的是将我的图像嵌套在 View 标签中,并为外部 View 和图像设置样式。键将宽度设置为 null,并将 resizeMode 设置为 'stretch'。下面是我的代码:
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',
}
});
使用 <ImageBackground>
正如 antoine129 所说。现在已弃用 <Image>
和 children。
class AwesomeClass extends React.Component {
render() {
return (
<ImageBackground source={require('image!background')} style={styles.container}>
<YourAwesomeComponent/>
</ImageBackground>
);
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
}
};
最新截至 17 年 10 月 (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
(RN >= .46)
the component cannot contain children if you want to render content on top of the image, consider using absolute positioning.
或者您可以使用 ImageBackground
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'
},
});
天哪,我终于找到了 React-Native V 0.52-RC 和 native-base 的好方法:
您的内容标签应该是这样的: //================================================ ==============
<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%'
}
效果很好朋友...玩得开心
2018 年 3 月更新不推荐使用图像使用 ImageBackground
<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
}
});
更新为 ImageBackground
由于暂时不推荐使用 <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'
}
});
ImageBackground 可能有限制
Actually, you can use directly and it is not deprecated.
如果您想在 React Native 中添加背景图片,并且还想在该背景图片上添加其他元素,请按照以下步骤操作:
- Create a Container View
- Create an Image element with 100% width and height. Also resizeMode: 'Cover'
- Create another View element under Image element with position: 'absolute'
这是我使用的代码:
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>
);
}
}
享受编码....
输出:
要处理此用例,您可以使用 <ImageBackground>
组件,它具有与 <Image>
相同的属性,并向其添加您想要在其上分层的任何子项。
示例:
return (
<ImageBackground source={...} style={{width: '100%', height: '100%'}}>
<Text>Inside</Text>
</ImageBackground>
);
更多:ImageBackground | React Native
Note that you must specify some width and height style attributes.
对我来说这很好用,我使用 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;
如果你想添加背景图片,你可以使用,但首先你必须从 'react-native' 导入它,如下所示:
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'
},
});