图像是垂直居中的,因为您在"样式"属性中添加了flex: 1
.不要添加flex:1,因为这会将图像填充到其父图像中,在本例中这是不需要的.
您应该始终在React Native中的图像上添加高度和宽度.如果图像总是相同的,可以使用Dimensions.get('window').width
来计算图像的大小.例如,如果比率始终为16x9,则高度为图像宽度的9/16.宽度等于设备宽度,因此:
const dimensions = Dimensions.get('window');
const imageHeight = Math.round(dimensions.width * 9 / 16);
const imageWidth = dimensions.width;
return (
<Image
style={{ height: imageHeight, width: imageWidth }}
/>
);
Note: When using an implementation like this, your image will not automatically resize when rotating your device, using split screen, etc. You will have to take care of those actions as well if you support multiple orientations...
如果比率不同,则根据每个不同图像的比率动态更改9/16.如果你真的不介意图像有点被裁剪,你也可以使用固定高度的封面模式:(https://snack.expo.io/rk_NRnhHb)
<Image
resizeMode={'cover'}
style={{ width: '100%', height: 200 }}
source={{uri: temp}}
/>