【问题标题】:React-Native display image without declaring dimensionsReact-Native 显示图像而不声明尺寸
【发布时间】:2017-06-07 12:32:40
【问题描述】:

在 React-Native 中,是否可以在不声明 widthheightstyle 属性的情况下显示图像?
我希望图像占据它拥有的所有空间或以其实际尺寸呈现。 (例如resizeMode是否设置为cover。)

我阅读了这篇文章并尝试实现它的方法但没有成功:https://medium.com/the-react-native-log/tips-for-react-native-images-or-saying-goodbye-to-trial-and-error-b2baaf0a1a4d

这是我的代码:

    <View>
      <Image
        source={{ uri: 'https://upload.wikimedia.org/wikipedia/en/c/c8/Marvelwolverine.jpg' }}
        style={{
          flex: 1,
          height: undefined,
          width: undefined,
        }}
      />
    </View>

但除非我指定硬编码 widthheight,否则不会打印任何内容。

【问题讨论】:

    标签: image react-native react-native-flexbox


    【解决方案1】:

    你可以试试

    <View style={{ flex: 1 }}>
                <Image source={{ uri:'http://...'}}
                    style={{
                        alignSelf: 'stretch',
                        flex: 1,
                    }}
                />
            </View>
    

    【讨论】:

    • resizeMode 不正确,但 alignSelf 按预期工作。检查:snack.expo.io/ryhO6dSf-
    • 我不确定..我用 resizeMode 得到了一些结果。将其设置为“中心”或“封面”等...这是我尝试过的一些更改:snack.expo.io/BJFA-FBzZ。似乎图像以其实际大小呈现,但它是居中的...感谢您展示@ 987654323@ !很有用。我觉得答案很好。但是我相信这个图像渲染的东西还有更多的深度......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多