为了得到一个圆,您需要创建一个正方形(宽度 === 高度)并将 borderRadius 设置为其宽度或高度的一半。
在你的情况下;您需要计算仅显示底部 30% 的值(使用负 marginTop),并计算确保圆心与屏幕宽度中心相同的值(使用负边距左)。下面是一个例子。
render() {
return (
<View style={myStyle.container}>
<View style={myStyle.top_background}>
<View style={myStyle.top_content}>
<Text style={myStyle.text1}>Hey there</Text>
<Text style={myStyle.text1}>WHAT'S UP</Text>
<Text style={myStyle.text1}>Doc'?</Text>
<Text style={myStyle.text2}>BUGS BUNNY</Text>
</View>
</View>
</View>
);
}
和样式表
const sWidth = Dimensions.get('window').width;
const sHeight = Dimensions.get('window').height;
const ratio = sWidth / sHeight; //sWidth = ratio * sHeight
const myStyle = StyleSheet.create({
container: {
width: sWidth,
height: sHeight,
backgroundColor: '#fff'
},
top_background: {
width: sHeight * 2,
height: sHeight * 2,
borderRadius: sHeight * 1,
borderTopLeftRadius: 0,
borderTopRightRadius: 0,
backgroundColor: '#aaa',
alignItems: 'center',
marginLeft: ((ratio * sHeight) * 0.5) - (sHeight),
marginTop: -sHeight * 1.7,
paddingTop: sHeight * 1.7,
},
top_content: {
paddingTop: sHeight * 0.02,
width: sWidth,
height: sHeight * 0.3,
alignItems: 'center',
},
text1: {
fontSize: 14,
color: '#fff'
},
text2: {
marginTop: sHeight * 0.1,
fontSize: 25,
fontWeight: 'bold',
color: '#fff'
}
});
top_background 中的 marginTop 和 paddingTop 仅用于使用屏幕前 30% 的部分,分别在屏幕中可以看到的部分开始内容。