【发布时间】:2020-03-31 08:16:20
【问题描述】:
在我的 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 和flexShrink: 1 添加到Image 中,但都没有产生任何影响。
有人知道我该如何处理吗?
注意:
如果我删除 Image 上的 height: 100%,我会得到:
更新:
澄清一下,这就是我想要的样子。我已经删除了这里的黑色边框。我通过添加top: -95 将它移到我想要的位置,但这通常不起作用,因为不同设备的值会有所不同:
【问题讨论】:
-
你能给我们看一张图片,我们可以看到你想要实现的目标吗?
标签: reactjs react-native react-native-image