【问题标题】:Not able to display static images in android with react-native无法使用 react-native 在 android 中显示静态图像
【发布时间】:2016-10-03 13:41:57
【问题描述】:

我正在尝试在我的 react-native android 和 iOS 应用程序中显示静态图像。它适用于 iOS 应用程序。但我无法在 android 应用程序中显示图像。以下是我的代码:

<Image source ={require('../images/background.png')}
              style ={styles.imageStyle}>
</Image>

var styles = StyleSheet.create({
  container: {
    flex: 1,
    alignSelf: 'stretch'
  },
  imageStyle: {
    flex: 1,
    margin:10,
    width: undefined,
    height: undefined,
    backgroundColor:'transparent',
    justifyContent: 'center',
    alignItems: 'center'

  }

我已将图像“background.png”放在 drawable-**dpi 文件夹以及上述图像文件夹下。

它适用于我的 iOS 应用程序从图像文件夹中选择图像,但不适用于 android。 我正在使用 react-native 版本 0.33

我们将不胜感激任何帮助!

编辑: 它在这里也不起作用:

<View key={answer.ID}>
  <OnTouchHighlightWidget key={answer.ID} onPress={() =>  {this.props.onSelect(answer)}}>
           <Image source={this.getImageUrl(answer)} style={{flex:1,margin:10,width: 90, height: 95}} />
</OnTouchHighlightWidget>
</View>

我已将 image_holo.png 与 js 文件和相应的 drawable-**dpi 文件夹放在同一目录下。它适用于 iOS。但不是在安卓上!

【问题讨论】:

  • 这是全封面背景图?
  • 是的。这是一张背景图片。

标签: android react-native


【解决方案1】:

您需要在您的imageStyle 中定义widthheight

【讨论】:

  • 感谢您的建议,但我已经这样做了。没有成功。
【解决方案2】:

试试这个以获得完整的背景图片封面。

<Image source ={require('../images/background.png')}
              style ={styles.imageStyle}>
</Image>

var styles = StyleSheet.create({
  container: {
    flex: 1,
    alignSelf: 'stretch'
  },
  imageStyle: {
    flex: 1,
    margin:10,
    width: null,
    height: null,
    backgroundColor:'transparent',
    justifyContent: 'center',
    alignItems: 'center'

  }

【讨论】:

  • 不。它不起作用。我在其他地方使用了我的问题中更新的图像。但那里也行不通。它适用于 iOS,但不适用于 android :(
  • 你没有添加宽度和高度吗?所以你什么也没看到?如果是的话,我会看看我认为有一次会遇到这个问题
  • 你的图片是否在视图中?
  • 是的,兄弟。它总是在 View 标签中。很抱歉没有发布完整的代码。现在更新了。即使是较早的一个也在 View 标签内。但它是如何在 iOS 上而不是在 android 上工作的呢?
  • 我不知道,抱歉,使用 react-native 有时 ios 有时 android 会很痛苦。
【解决方案3】:

尝试在您的android Studio 中的res 文件夹下创建一个drawable 文件夹并将您的图像复制到此文件夹,请参考下面的链接进行设置(Android Studio: Drawable Folder: How to put Images for Multiple dpi?)。 在 android studio 上清理并重建您的项目,它可以工作。

【讨论】:

    猜你喜欢
    • 2015-12-17
    • 2016-11-02
    • 2016-12-20
    • 2019-05-29
    • 2022-01-01
    • 2015-12-12
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    相关资源
    最近更新 更多