React本机Image组件在android中不起作用

时间:2016-07-06 13:15:22

标签: android image react-native

我对React Native很新,我在使用Image时遇到了问题。

为了简化我的问题,我创建了一个新的反应本机项目,并在index.android.js中添加了Image组件,如下所示:

/**
 * 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);

Image组件中的网址是从React Native Image documentation复制的。 我究竟做错了什么?有人可以帮帮我吗?

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

3 个答案:

答案 0 :(得分:1)

给出图像的高度和宽度。

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

答案 1 :(得分:0)

尝试使用宽度和高度为图像添加样式。

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

答案 2 :(得分:0)

谢谢。我刚刚添加了样式并将大小指定为{{height:30,width:30}},现在它可以使用了!