【发布时间】:2017-01-16 01:02:46
【问题描述】:
我正在开发一个示例反应原生项目。除了<Image source=''/> 之外,几乎所有功能都可以很好地使用它。该图像在 android studio 和 genymotion 提供的 android 模拟器中显示良好,但不适用于任何真实设备(moto G3 turbo、nexus 5、galaxy s4 等)。我不知道我的代码出了什么问题。这是我的代码
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image
} from 'react-native';
class ImageTester extends Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Text style={styles.instructions}>
To get started, edit index.android.js
</Text>
<Text style={styles.instructions}>
Double tap R on your keyboard to reload,{'\n'}
Shake or press menu button for dev menu
</Text>
<Image source={require('./img/first_image.png')}></Image>
</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('ImageTester', () => ImageTester);
项目结构:
反应原生版本:反应原生:0.32.1
【问题讨论】:
-
@luwu 我已经经历过了。它对我不起作用。
-
@luwu 你是对的。问题出在 bundle 打包命令上。我已经解决了。我将其发布在下面作为自我回答。感谢您的所有帮助。:-)
-
你需要设置
width,heightofImage这样的组件<Image source={require('./img/first_image.png')} style={{ width: 100, height: 100 }}></Image> -
@ubatin 如果您使用的是本地图像,则不需要指定图像尺寸。看看这些例子:-facebook.github.io/react-native/releases/0.33/docs/image.html
标签: android image android-emulator react-native