首页 > 解决方案 > 将背景图像添加到 create-react-native 项目

问题描述

我正在尝试将背景图像插入到我的 create-react-native 项目中。我希望能够添加这张图片:https ://i.pinimg.com/736x/80/29/a9/8029a9bf324c79b4803e1e5a2aba25f3--costume-makeup-iphone-wallpaper.jpg 。我尝试将它应用于样式表,但它不接受背景图像标签。我对反应很陌生。

import React from 'react';
import { StyleSheet, Text, View, TextInput, Button} from 'react-native';
import ListItem from "/Users/Westin/assignment5/ListItem";


export default class App extends React.Component {
  state ={
    thing: "",
    things: [],
  };

  thingValueChanged = value =>{
    //alert(value);
    this.setState({
      thing: value
    });
  }

  onClickingAdd = () =>
  {
    if(this.state.thing === "")
    {
      return;
    }

    this.setState(prevState => {
        return {
          things: prevState.things.concat(prevState.thing)
        };

    });
  }



  render() {
    const thingsOut = this.state.things.map((thing,i) => (<ListItem key = {i} thing={thing} />))
    
    
    return (
      <View style={styles.container}>
      <View style={styles.header}>
       <Text style={styles.headerText}>My Favourite Things</Text>
      </View>

      <View style={styles.input}>
      <TextInput 
      value={this.state.thing}
      placeholder="Add your favourite things" 
      style={styles.inputbox}
      onChangeText={this.thingValueChanged}
      />
      <Button
      title="Add"
      style={styles.addButton}
      onPress = {this.onClickingAdd}
      
      />
      </View>
      <View>
        {thingsOut}
        </View>


      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#e6eeff',
    alignItems: 'center',
    justifyContent: 'flex-start',
    paddingTop: 30,
  },
  header: {
    padding: 10,
    
  },
  headerText: {
    fontSize: 30,
    color: '#003cb3',
  },
  inputbox: {
    borderWidth: 1,
    height: 40,
    width: "70%",
  },
  addButton: {
    width: "30%"
  },
  input: {
    flexDirection: "row",
    width: '100%',
    justifyContent: "space-evenly",
    alignItems: "center",
  }
});

这是我试图运行的代码

import React from 'react';
import { StyleSheet, Text, View, TextInput, Button, Image } from 'react-native';
import ListItem from "/Users/Westin/assignment5/ListItem";

const remote = 'https://i.pinimg.com/736x/80/29/a9/8029a9bf324c79b4803e1e5a2aba25f3--costume-makeup-iphone-wallpaper.jpg';

export default class BackgroundImage extends Component {
  render() {
    const resizeMode = 'center';

    return (
      <Image
        style={{
          flex: 1,
          resizeMode,
        }}
        source={{ uri: remote }}
      />
    );
  }
}

AppRegistry.registerComponent('BackgroundImage', () => BackgroundImage);

export default class App extends React.Component {
  state ={
    thing: "",
    things: [],
  };

  thingValueChanged = value =>{
    //alert(value);
    this.setState({
      thing: value
    });
  }

  onClickingAdd = () =>
  {
    if(this.state.thing === "")
    {
      return;
    }

    this.setState(prevState => {
        return {
          things: prevState.things.concat(prevState.thing)
        };

    });
  }



  render() {
    const thingsOut = this.state.things.map((thing,i) => (<ListItem key = {i} thing={thing} />))
    
    
    return (
      <View style={styles.container}>
      <View style={styles.header}>
       <Text style={styles.headerText}>My Favourite Things</Text>        
      </View>

      <View style={styles.input}>
      <TextInput 
      value={this.state.thing}
      placeholder="Add your favourite things" 
      style={styles.inputbox}
      onChangeText={this.thingValueChanged}
      />
      <Button
      title="Add"
      style={styles.addButton}
      onPress = {this.onClickingAdd}
 
      />
      </View>
      <View>
        {thingsOut}
        </View>


      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    backgroundColor: 'black',
    opacity: 0.5,
    justifyContent: 'flex-start',
    paddingTop: 30,
  },
  header: {
    padding: 10,
  },
  headerText: {
    fontSize: 30,
    color: '#003cb3',
  },
  inputbox: {
    borderWidth: 1,
    height: 40,
    width: "70%",
    backgroundColor: 'white',
    },
  addButton: {
    width: "30%"
  },
  input: {
    flexDirection: "row",
    width: '100%',
    justifyContent: "space-evenly",
    alignItems: "center",
  }
});

它说我不能运行两个导出类

标签: javascriptreact-native

解决方案


这段代码对我有用:

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

export default class App extends React.Component {
    render() {
        return (
                <ImageBackground source=
                { {uri: 'https://i.pinimg.com/736x/80/29/a9/8029a9bf324c79b4803e1e5a2aba25f3--costume-makeup-iphone-wallpaper.jpg' } }
            style={styles.container}
                >
                <Text>Some</Text>
                <Text>Text</Text>
        <Text>Centered</Text>
        <Text>In</Text>
                <Text>Columns</Text>
                </ImageBackground>
    );
  }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#fff',
        alignItems: 'center',
        justifyContent: 'center',
        width: '100%'
    },
});

您可以在此处阅读更多信息<ImageBackground/>https ://facebook.github.io/react-native/docs/images#background-image-via-nesting

安卓截图


推荐阅读