反应原生图像组件在android中不工作

问题描述:

我对React Native非常陌生,我遇到了使用Image的问题。反应原生图像组件在android中不工作

为了简化我的问题,我创建了一个新的反应本地项目和刚添加图像组件在如下index.android.js:

/** 
* Sample React Native App 
* https://github.com/facebook/react-native 
* @flow 
*/ 

import React, { Component } from 'react'; 
import { 
    AppRegistry, 
    StyleSheet, 
    Text, 
    View, 
    Image, 
} from 'react-native'; 

class TestProject extends Component { 
    render() { 
    return (
     <View style={styles.container}> 
     <Text style={styles.welcome}> 
      Welcome to React Native! 
     </Text> 
     <Image source={{uri: 'http://facebook.github.io/react/img/logo_og.png'}}/> 
     <Text style={styles.instructions}> 
      To get started, edit index.android.js 
     </Text> 
     <Text style={styles.instructions}> 
      Shake or press menu button for dev menu 
     </Text> 
     </View> 
    ); 
    } 
} 

const styles = StyleSheet.create({ 
    container: { 
    flex: 1, 
    justifyContent: 'center', 
    alignItems: 'center', 
    backgroundColor: '#F5FCFF', 
    }, 
    welcome: { 
    fontSize: 20, 
    textAlign: 'center', 
    margin: 10, 
    }, 
    instructions: { 
    textAlign: 'center', 
    color: '#333333', 
    marginBottom: 5, 
    }, 
}); 

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

图像组件内的URL来自React Native Image documentation复制。 我在做什么错?任何人都可以请帮我吗?

我的反应原生是0.29.0,我还没有尝试过这在ios中,但因为现在我需要先创建一个Android应用程序。

+0

你需要给图像的宽度和高度渲染...作为 style = {{height:30,width:30}} – Riten

+0

谢谢你的答案。这可能是因为我还没有尝试过。我会尝试设置图像的大小,看看它是否工作。 –

尝试添加样式到您的图像的宽度和高度。

对于本机下载图像的反应,获取其大小,然后再次渲染会导致糟糕的用户体验。您需要指定尺寸。

给出图像的高度和宽度。

<Image source={{uri: 'http://facebook.github.io/react/img/logo_og.png'}} style={{ height: 50, width: 50 }}/> 

谢谢。我刚刚添加了样式,并将尺寸指定为{{height:30,width:30}},现在它可以工作!