react-native - 如何在两个背景视图之间实现浮动徽标布局?
问题描述
我正在尝试实现类似于此图像的第二个布局的布局我希望背景图像占据屏幕的 40%,然后在其下方显示一个包含我的内容的视图,但我希望我的徽标浮动在二。类似于“G”位于图像和内容之间的方式。我尝试有两个视图并将徽标嵌套在第一个视图中并尝试定位 absolute 但我无法将其水平居中,使其成为宽度的 10% 并具有完美的圆形,因为您似乎无法在绝对定位元素。
这是我的尝试
解决方案
要使徽标居中,您只需将其放在具有屏幕宽度的视图中并将 alignItems 设置为“居中”即可。以下是如何进行此布局的简单示例:
render() {
return (
<View style={myStyle.container}>
<View style={myStyle.topBanner} />
<View style={myStyle.contentArea}>
<Text style={myStyle.contentTitle}>HERE LIES CONTENT</Text>
</View>
<View style={myStyle.midLogoRow}>
<Image style={myStyle.midLogo} source={require('../../images/profile_achive.png')} />
</View>
</View>
);
}
样式表如下:
const sWidth = Dimensions.get('window').width;
const sHeight = Dimensions.get('window').height;
const myStyle = StyleSheet.create({
container: {
flex: 1,
width: sWidth,
backgroundColor: '#fff',
},
topBanner: {
width: sWidth,
height: sHeight * 0.4,
backgroundColor: '#c5c5c5',
},
midLogoRow: {
position: 'absolute',
top: sHeight * 0.3,
left: 0,
width: sWidth,
alignItems: 'center',
},
midLogo: {
width: sHeight * 0.18,
height: sHeight * 0.18,
},
contentArea: {
marginTop: sHeight * 0.09,
alignItems: 'center'
},
contentTitle: {
fontSize: 18,
color: '#333',
fontWeight: 'bold',
},
});
如你看到的; 您的徽标所在的行(在我的情况下是 profile_achieve.png)放置在最低部分,以便将其呈现在其他所有内容之上。contentArea 的上边距设置为屏幕高度的 0.09,即徽标高度除以 2。
推荐阅读
- date - DAX 中按日期和日期范围分组的列中一个值的总计百分比
- arrays - 根据数组验证字符串
- php - 如何从服务提供者类访问经过身份验证的用户?
- python - Python - http.client:对 TMDB API 的 GET 请求失败错误
- azure - 为什么在委派的 az 广告应用程序上需要秘密?
- python - 如何扩展字典中的现有键?
- selenium-webdriver - 我需要一个 gradle 任务来执行我的 Cucumber Runner 类并执行 Cucumber tets
- python - Python遥测 - 记录所有加载的python模块
- go - 无论平台如何,如何在所有代码上运行 go vet
- r - 导入csv文件的麻烦