React Native 仅当高度和宽度为固定值时才显示图像

uwopmtnx  于 2023-10-22  发布在  React
关注(0)|答案(2)|浏览(172)

这是最令人沮丧的事情。当我通过image style属性将图像的宽度和高度设置为“100%”时,图像不显示:

但是,当我将其设置为固定值时:

下面是我的render():

  1. render() {
  2. const { images } = this.props;
  3. return (
  4. <View style={styles.container}>
  5. {images.map((image, index) => (
  6. <Animated.Image
  7. {...this.panResponder.panHandlers}
  8. key={index}
  9. source={{ uri:`${encodeURI(image)}` }}
  10. style={[
  11. styles.image,
  12. // {
  13. // transform: [{ translateX: this.pan.x }],
  14. // zIndex: index === this.state.currentIndex ? 1 : 0,
  15. // },
  16. ]}
  17. />
  18. ))}
  19. </View>
  20. );
  21. }
zxlwwiss

zxlwwiss1#

如果将图像的widthheight设置为100%,则表示图像具有与其父组件相同的widthheight
在本例中,您没有将size设置为父组件,因此图像的widthheight等于0

gkn4icbw

gkn4icbw2#

尝试将父viewwidthheight设置为'100%',而不是flex: 1,或者仅在父flex: 1中添加另一个视图,height'100%'width
EX.

  1. <View style={styles.container}>
  2. <View style={{height: '100%', width: '100%'}}>
  3. {images.map((image, index) => (
  4. <Animated.Image
  5. {...this.panResponder.panHandlers}
  6. key={index}
  7. source={{ uri:`${encodeURI(image)}` }}
  8. style={[
  9. styles.image,
  10. // {
  11. // transform: [{ translateX: this.pan.x }],
  12. // zIndex: index === this.state.currentIndex ? 1 : 0,
  13. // },
  14. ]}
  15. />
  16. ))}
  17. </View>
  18. </View>
展开查看全部

相关问题