reactjs - 无法在本机反应中设置我的背景图像?
问题描述
我是新来的本地人,在学习本地文档时,我被 ImageBackground 的使用困住了。我的代码在下面给出,帮助将不胜感激。它工作正常,但是当我开始使用导航器功能时,它无法正常工作。请提出解决此问题的最佳解决方案。
import React from 'react';
import { StyleSheet, Text, View, Button, ImageBackground } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
class HomeScreen extends React.Component {
render() {
return (
<View style={styles.container}>
<ImageBackground
source={{
uri:
'https://images.unsplash.com/photo-1524338198850-8a2ff63aaceb?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=358&q=80',
}}
style={styles.backgroundContainer}/>
<Text style={styles.welcomenote}>WELCOME TO ME</Text>
<View style={styles.loginbtn}>
<Button style={styles.loginbtn} title="LOGIN" />
</View>
<View style={styles.signupbtn}>
<Button title="SIGNUP" />
</View>
</View>
);
}
}
class DetailsScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
</View>
);
}
}
const RootStack = createStackNavigator(
{
Home: HomeScreen,
Details: DetailsScreen,
},
{
initialRouteName: 'Home',
}
);
const AppContainer = createAppContainer(RootStack);
export default class App extends React.Component {
render() {
return <AppContainer />;
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
backgroundContainer: {
flex: 1,
width: 300,
height: 100,
resizeMode: 'cover',
justifyContent: 'center',
alignItems: 'center,',
},
loginbtn: {
padding: 20,
width: '90%',
height: 40,
position: 'absolute',
left: 20,
right: 0,
top: 250,
bottom: 0,
},
signupbtn: {
padding: 20,
width: '90%',
height: 40,
position: 'absolute',
left: 20,
right: 0,
top: 200,
bottom: 0,
},
welcomenote: {
position: 'absolute',
left: 70,
right: 0,
top: 200,
bottom: 0,
width: '90%',
height: 40,
color: 'white',
},
});
一切都很好,它没有显示背景图像
解决方案
尝试这个,
import React from 'react';
import { StyleSheet, Text, View, Button, ImageBackground } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
class HomeScreen extends React.Component {
render() {
return (
<View style={styles.container}>
<Image source={require('https://images.unsplash.com/photo-1524338198850-8a2ff63aaceb?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=358&q=80')} style=
{styles.backgroundContainer}/>
<Text style={styles.welcomenote}>WELCOME TO ME</Text>
<View style={styles.loginbtn}>
<Button style={styles.loginbtn} title="LOGIN" />
</View>
<View style={styles.signupbtn}>
<Button title="SIGNUP" />
</View>
</View>
);
}
}
class DetailsScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
</View>
);
}
}
const RootStack = createStackNavigator(
{
Home: HomeScreen,
Details: DetailsScreen,
},
{
initialRouteName: 'Home',
}
);
const AppContainer = createAppContainer(RootStack);
export default class App extends React.Component {
render() {
return <AppContainer />;
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
backgroundContainer: {
flex: 1,
width: 300,
height: 100,
resizeMode: 'cover',
justifyContent: 'center',
alignItems: 'center,',
},
loginbtn: {
padding: 20,
width: '90%',
height: 40,
position: 'absolute',
left: 20,
right: 0,
top: 250,
bottom: 0,
},
signupbtn: {
padding: 20,
width: '90%',
height: 40,
position: 'absolute',
left: 20,
right: 0,
top: 200,
bottom: 0,
},
welcomenote: {
position: 'absolute',
left: 70,
right: 0,
top: 200,
bottom: 0,
width: '90%',
height: 40,
color: 'white',
},
});
推荐阅读
- c# - MVVM Light - 如何检测某些属性是否已更改
- css - 在保持默认滑动行为的情况下,如何通过移动触摸使 Bootstrap 轮播可缩放/可缩放?
- ssas - 维度的 ProcessUpdate 触发处理多维数据集中所有度量值组的所有分区
- azure-ad-b2c - 在 Azure AD B2C 中的注册过程中收集/上传文件作为自定义属性
- ios - 如何解决 iphone x 上的约束问题?
- node.js - 如何通过在mongoose nodejs的聚合中使用相同的嵌套对象值来对数组对象进行分组
- npm - TestCafe & chrome:headless : 如何强制浏览器语言(语言环境)
- menu - Glowscript 中的菜单
- java - 编译错误“Outer.Nested has private access in Outer”以嵌套类为值的外部类上的注释
- c# - 在按键上更改数据模板并将密钥发送到模板