在我的 React Native 应用程序中,我有一个高度为 300 的红色框,我想垂直居中,以及一个我想坐在这个红色框内部和顶部的图像。到目前为止,这是我的代码:
import React, { Component } from 'react';
import { View, Image} from 'react-native';
export default class Login extends Component {
render() {
return (
<View style={{
flex: 1,
justifyContent: "center",
alignItems: "center"
}}>
<View
style={{
borderWidth: 3,
borderColor: 'red',
width: "100%",
height: 300
}}
>
<Image
source={require('../../images/swoord-top.png')}
style={{
width: "100%",
height: "100%",
resizeMode: "contain",
borderWidth: 3
}}
>
</Image>
</View>
</View>
);
}
}
这是这样的:
红框是我提到的外框,黑框只是Image
. 问题是这个黑盒子(即Image
)扩展以垂直填充红色盒子,并且图像在这个黑盒子内垂直居中,所以它在红色盒子内垂直居中,而不是在flex-start
我想要它的位置. 我已经尝试添加justifyContent: flex-start
andflexShrink: 1
并且Image
两者都没有改变。
有谁知道我该如何解决这个问题?
笔记:
如果我删除height: 100%
on Image
,我会得到:
更新:
澄清一下,这就是我希望它看起来的样子。我已经删除了这里的黑色边框。我通过添加将它移到我想要的位置top: -95
,但这通常不起作用,因为不同设备的值会有所不同: