tal*_*paz 134 javascript react-native
我想在View中添加一个全屏图像,所以我编写了这段代码
return (
<View style={styles.container}>
<Image source={require('image!egg')} style={styles.backgroundImage}>
</View>
)
Run Code Online (Sandbox Code Playgroud)
并将样式定义为
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
flexDirection: 'column',
},
backgroundImage:{
width:320,
height:480,
}
...
Run Code Online (Sandbox Code Playgroud)
但是这样我怎么能找到实际的iPhone屏幕尺寸?
我见过一个API来访问像素密度,但没有关于屏幕尺寸......
任何的想法?
oro*_*nbz 171
(现已弃用,现在可以使用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,
}
};
Run Code Online (Sandbox Code Playgroud)
Jos*_*osh 101
您可以flex: 1
在<Image>
元素上使用样式使其填满整个屏幕.然后,您可以使用图像调整大小模式之一使图像完全填充元素:
<Image source={require('image!egg')} style={styles.backgroundImage} />
Run Code Online (Sandbox Code Playgroud)
样式:
import React from 'react-native';
let { StyleSheet } = React;
let styles = StyleSheet.create({
backgroundImage: {
flex: 1,
resizeMode: 'cover', // or 'stretch'
}
});
Run Code Online (Sandbox Code Playgroud)
我很确定你可以摆脱<View>
包装你的形象,这将有效.
Vin*_*ale 41
注意:此解决方案已过时.请参考 https://facebook.github.io/react-native/docs/images.html#background-image-via-nesting代替
试试这个解决方案 它得到官方支持.我刚刚测试过它并且完美无缺.
var styles = StyleSheet.create({
backgroundImage: {
flex: 1,
alignSelf: 'stretch',
width: null,
}
});
Run Code Online (Sandbox Code Playgroud)
至于将它用作背景图像,只需执行以下操作即可.
<Image style={styles.backgroundImage}>
<View>
<Text>All your stuff</Text>
</View>
</Image>
Run Code Online (Sandbox Code Playgroud)
小智 19
我使用react-native version = 0.19.0尝试了几个这样的答案无法用于android.
出于某种原因,我的样式表中的resizeMode不能正常工作?然而,当sytlesheet有
backgroundImage: {
flex: 1,
width: null,
height: null,
}
Run Code Online (Sandbox Code Playgroud)
并且,在Image标签中我指定了resizeMode:
<Image source={require('path/to/image.png')} style= {styles.backgroundImage} resizeMode={Image.resizeMode.sretch}>
Run Code Online (Sandbox Code Playgroud)
它工作得很好!如上所述,您可以使用Image.resizeMode.cover或包含.
希望这可以帮助!
Rya*_* Wu 12
根据Braden Rockwell Napier的回答,我制作了这个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
Run Code Online (Sandbox Code Playgroud)
someWhereInMyApp.js
import BackgroundImage from './backgroundImage'
....
<BackgroundImage source={ { uri: "https://facebook.github.io/react-native/img/header_logo.png" } }>
<Text>Test</Text>
</BackgroundImage>
Run Code Online (Sandbox Code Playgroud)
Hit*_*ahu 12
<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 >
Run Code Online (Sandbox Code Playgroud)
哦,上帝终于找到了React-Native V 0.52-RC和原生基地的好方法:
您的内容标记应该是这样的:// ======================================= =======================
<Content contentContainerStyle={styles.container}>
<ImageBackground
source={require('./../assets/img/back.jpg')}
style={styles.backgroundImage}>
<Text>
Some text here ...
</Text>
</ImageBackground>
</Content>
Run Code Online (Sandbox Code Playgroud)
而你的基本风格是:// ========================================== ====================
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
backgroundImage:{
flex : 1,
width : '100%'
}
Run Code Online (Sandbox Code Playgroud)
它很好的朋友......玩得开心
更新到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>
Run Code Online (Sandbox Code Playgroud)
https://github.com/facebook/react-native/blob/master/Libraries/Image/ImageBackground.js
小智 8
import { ImageBackground } from "react-native";
<ImageBackground
style={{width: '100%', height: '100%'}}
source={require('../assets/backgroundLogin.jpg ')}> //path here inside
<Text>React</Text>
</ImageBackground>
Run Code Online (Sandbox Code Playgroud)
对我来说这很好用,我使用 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;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
160710 次 |
最近记录: |