0

我对expo和react native完全陌生我正在尝试使用ImageBackground我将它用作下面的代码,但是我从“app\components\Login. js" " 我的图像已经在我的项目的资产文件夹中而且当我尝试导入字体时,我得到同样的错误是否需要在使用之前导入一些东西或其他东西?我也尝试过不导入图像并将路径直接添加到源属性

登录.js

import React, { Component } from 'react';
import back from './assets/back.jpg';
import {
    StyleSheet,
    Text,
    View,
    TextInput,
    KeyboardAvoidingView,
    TouchableOpacity,
    AsyncStorage,
    Image,
    ImageBackground,
} from 'react-native';
import {createStackNavigator, createAppContainer} from 'react-navigation';
export default class Login extends React.Component {
    static navigationOptions = {
        header: null,
    };
    constructor(props) {
        super(props);
        this.state = {
            username:'',
            password:'',
        }
    }
    componentDidMount(){
        this._loadInitialState().done();
    }
    _loadInitialState = async () =>{
        var value = await AsyncStorage.getItem('user');
        if (value !=null){
            this.prop.navigation.navigate('Profile');
        }
    }
    render() {
        return (
            <ImageBackground source={back} style={styles.backImg}>
                <KeyboardAvoidingView behavior='padding' style={styles.wrapper}>
                <View style={styles.container}>
                <Image
                    style={styles.logo}
                    source={require('/MyFirst/assets/logo.png')}
                />
                <TextInput
                    style={styles.textInput} placeholder='Username'
                    onChangeText={(username) =>this.setState({username}) }
                    underlineColorAndroid = 'transparent'
                        />
                        <TextInput
                    style={styles.textInput} placeholder='Password'
                    onChangeText={(password) =>this.setState({password}) }
                    underlineColorAndroid = 'transparent'
                        />
                        <TouchableOpacity style={styles.btn}
                    onPress={this.login}>
                <Text style={styles.btnText}>Login</Text>
                    </TouchableOpacity>
                    </View>
                    </KeyboardAvoidingView>
            </ImageBackground>

        );
    }
}
const styles = StyleSheet.create({
    wrapper:{
        flex:1,
    },
    container:{
        flex:1,
        alignItems:'center',
        justifyContent:'center',
        backgroundColor:'#e5ffd3',
        paddingLeft:40,
        paddingRight:40,
    },
    logo:{
        width: 250,
        height: 250,
    },
    textInput:{
        alignSelf:'stretch',
        padding:16,
        marginBottom:20,
        backgroundColor:'#fff',
        borderWidth: 1,
        borderColor: '#d6d7da',
        borderRadius: 25,
    },
    btn:{
        alignSelf:'stretch',
        backgroundColor:'#589e25',
        padding:20,
        alignItems: 'center',
        borderRadius: 25,
    },
    btnText:{
        color: '#ffffff',
    },
})

应用程序.js

import React, { Component } from 'react';
import {
    StyleSheet,
    Text,
    View
} from 'react-native';
import {createStackNavigator, createAppContainer} from 'react-navigation';
import Login from './app/components/Login'
import Profile from './app/components/Profile'
const MainNavigator = createStackNavigator({
    Home: {screen: Login},
    Profile: {screen: Profile},
});
const App = createAppContainer(MainNavigator);
export default App;
4

1 回答 1

1

你应该ImageBackground以这种方式使用。它会解决你的问题

 <ImageBackground source={require('../../assets/back.jpg')} style={styles.backImg}>

工作示例可以在这里找到链接

于 2019-04-12T17:41:19.857 回答