【问题标题】:Images not displaying in ListView图像未显示在 ListView 中
【发布时间】:2016-11-01 08:11:15
【问题描述】:
 var testData=[{"name":"require('./Images/X.jpg')"},{"name":"require('./Images/Y.jpg')"}];

 class SampleRow extends React.Component{
 render() {
  return (
  <View style={styles.wrapper}>
    <View>
            <Image style={styles.Img} source={this.props.name}></Image> 
            <Text style={styles.text}>Hello</Text>
    </View>
   </View>
);
}
}

class ListViewRows extends React.Component{
 constructor() { super(); const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}); this.state = { dataSource: ds.cloneWithRows(testData), }; }


 renderRow(rowData) {
return <SampleRow name={rowData.name} style={styles.row} />
 }

 render() {
  return (
  <ListView
    ref="listView"
    style={styles.container}
    dataSource={this.state.dataSource}
    renderRow={this.renderRow}
  />
);
 }
}


var styles = StyleSheet.create({
 container: {
flex: 1,
backgroundColor: '#F5FCFF',
padding: 20
 },
 wrapper: {
flexDirection: 'row',
justifyContent: 'space-between',
paddingRight: 10,
borderBottomWidth: 1,
borderBottomColor: '#e9e9e9',
marginTop: 20
 },
 text: {
fontSize: 24,
fontWeight: "100",
color: 'black',
  },Img:{
 width:200,height:200,resizeMode: 'contain'
  },
  });

  AppRegistry.registerComponent('List', () => ListViewRows); 

我正在尝试使用图像创建列表视图。试图复制这个https://rnplay.org/apps/d3DM6A 。在我的输出图像中没有出现。我裁判React-native failed propType on Image component 并将来源更改为

source={{uri: this.props.name}} 还是不行。请帮忙。

【问题讨论】:

    标签: react-native


    【解决方案1】:

    替换

     var testData=[{"name":"require('./Images/X.jpg')"},{"name":"require('./Images/Y.jpg')"}];
    

     var testData=[{"name":require('./Images/X.jpg')},{"name":require('./Images/Y.jpg')}];
    

    【讨论】:

    • @Jickson 它有效。我以前也试过,主要问题是我的 Geny Motion 多次显示以前的输出,即使我更改了代码,所以我认为代码中有一些错误。我重新启动了 Geny Motion 几次,它终于可以工作了。我还有一个问题,涉及大量静态图像的应用程序在开发过程中变得越来越慢。特别是导航变得越来越困难。如何克服这个
    • 抱歉,没有看到代码就很难找到原因。用部分代码创建一个问题,我们可以用它来重新创建问题。希望我们届时能够为您提供帮助。
    • 图像文件的大小会影响应用程序的性能。我将图像的尺寸减小到更低的 KB 现在可以正常工作了。
    • 酷。很高兴你找到了解决方案:)
    猜你喜欢
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    相关资源
    最近更新 更多