无法在 React Native 中设置我的背景图片?
unable set my background image in react native?
我是 React Native 的新手,在学习原生文档时,我被 ImageBackground 的用法卡住了。我的代码在下面给出,帮助将不胜感激。它工作正常,但是当我开始使用导航器功能时,它不起作用,因为 normal.please 建议解决此问题的最佳解决方案。
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',
},
});
您遇到了一些问题:
- alignItems: 'center,', <- ''
内的逗号
- imageBackground 组件是视图的父组件
- 从 'loginbtn'、'signupbtn' 等
移除高度、左上角
尝试使用所有 flex css(这将是响应式的);并且不要使用固定高度,或左或右或顶部
这里的代码工作并经过测试:
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 (
<ImageBackground style={styles.backgroundContainer}
source={{
uri:
'https://images.unsplash.com/photo-1524338198850-8a2ff63aaceb?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=358&q=80',
}}
>
<View style={styles.container}>
<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>
</ImageBackground>
);
}
}
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,
justifyContent: 'center',
alignItems: 'center',
},
loginbtn: {
padding: 20,
width: '90%',
position: 'absolute',
top: 250,
bottom: 0,
},
signupbtn: {
padding: 20,
width: '90%',
position: 'absolute',
top: 200,
bottom: 0,
},
welcomenote: {
position: 'absolute',
top: 200,
bottom: 0,
width: '90%',
height: 40,
color: 'white',
},
});
试试
import React from 'react';
import { StyleSheet, Text, View, Button, ImageBackground, Dimensions } 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,
alignItems: 'center',
justifyContent: 'center',
},
backgroundContainer: {
width: 300,
height: Dimensions.get('window').height
},
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,
},
});
在上面,当您将 backgroundContainer
的 height
设置为 100 时,它只需要 100
我是 React Native 的新手,在学习原生文档时,我被 ImageBackground 的用法卡住了。我的代码在下面给出,帮助将不胜感激。它工作正常,但是当我开始使用导航器功能时,它不起作用,因为 normal.please 建议解决此问题的最佳解决方案。
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',
},
});
您遇到了一些问题:
- alignItems: 'center,', <- '' 内的逗号
- imageBackground 组件是视图的父组件
- 从 'loginbtn'、'signupbtn' 等 移除高度、左上角
尝试使用所有 flex css(这将是响应式的);并且不要使用固定高度,或左或右或顶部
这里的代码工作并经过测试:
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 (
<ImageBackground style={styles.backgroundContainer}
source={{
uri:
'https://images.unsplash.com/photo-1524338198850-8a2ff63aaceb?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=358&q=80',
}}
>
<View style={styles.container}>
<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>
</ImageBackground>
);
}
}
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,
justifyContent: 'center',
alignItems: 'center',
},
loginbtn: {
padding: 20,
width: '90%',
position: 'absolute',
top: 250,
bottom: 0,
},
signupbtn: {
padding: 20,
width: '90%',
position: 'absolute',
top: 200,
bottom: 0,
},
welcomenote: {
position: 'absolute',
top: 200,
bottom: 0,
width: '90%',
height: 40,
color: 'white',
},
});
试试
import React from 'react';
import { StyleSheet, Text, View, Button, ImageBackground, Dimensions } 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,
alignItems: 'center',
justifyContent: 'center',
},
backgroundContainer: {
width: 300,
height: Dimensions.get('window').height
},
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,
},
});
在上面,当您将 backgroundContainer
的 height
设置为 100 时,它只需要 100