简体   繁体   English

图片不符合约束条件

[英]Image not respecting constraints

According to the documentation on Image , you can use resizeMode to have the image be contained within its container (in this case, a View). 根据Image上的文档 ,您可以使用resizeMode将图像包含在其容器(在本例中为View)中。

The code I have is this: 我的代码是这样的:

  <View style={{backgroundColor: '#ccf'}}>
    <Image
      resizeMode={Image.resizeMode.contain}
      source={require('../../../assets/ste-logo.png')}
    />
  </View>

Yet what I get is this: 但是我得到的是:

在此处输入图片说明

I would expect the logo to shrink down to fit within the purple box. 我希望徽标缩小以适合紫色框。 I can fix this by specifying a hard width on the image, but I don't want to do that as it should look good on all phone sizes. 我可以通过在图片上指定一个较硬的宽度来解决此问题,但是我不想这样做,因为它在所有手机尺寸上都应该看起来不错。 I basically want the image to stretch to 100% of its containing View. 我基本上希望图像可以拉伸到其包含View的100%。 How do I do this? 我该怎么做呢?

The Image component is itself a view (or box with regard to the box model layout). Image组件本身就是一个视图(或关于盒子模型布局的盒子)。 Since you didn't give it a size it just grows to fit it's contents. 由于您没有给它一个尺寸,它会随其内容而增长。 In this case it's then to big to fit it's parent and overflows. 在这种情况下,它会变得很大以适应其父级并溢出。

So you either you explicitly give the Image the desired width and hight or you use flex: 1. In that case have to give the container a flexDirection: 'row'. 因此,您要么显式地给Image提供所需的宽度和高度,要么使用flex:1.在这种情况下,必须给容器一个flexDirection:'row'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM