[英]fix a line's position in the center - scaled sheets
I am doing this to make a verticle line in between two numbers:我这样做是为了在两个数字之间画一条垂直线:
<View style={styles.ridesFriends}>
<Text style={styles.numbers}>132</Text>
<View style={styles.verticleLine}></View>
<Text style={styles.numbers}>2</Text>
</View>
ridesFriends: {
paddingTop: 60,
alignItems: 'center',
flexDirection: 'row',
justifyContent: 'space-evenly',
// marginLeft: 2,
// marginRight: 3,
width: '100%',
},
numbers: {
fontSize: 30,
color: '#31C283',
fontWeight: 'bold',
},
verticleLine: {
height: '100%',
width: 1,
backgroundColor: '#909090',
}
However, the line doesn't appear in the exact middle.但是,该线不会出现在确切的中间。 This is because 132 is longer than the number 2. If I change 132 to just 3, then the line appears in the center.
这是因为 132 比数字 2 长。如果我将 132 更改为 3,则该线出现在中心。 Is there any way to fix the line in the middle?
有什么办法可以修复中间的线吗?
<View style={styles.ridesFriends}>
{/* <View style={styles.numbersContainer}> */}
<Text style={styles.numbers}>132</Text>
<View style={styles.verticleLine}></View>
<Text style={styles.numbers}>{numberOfFriends}</Text>
{/* </View> */}
</View>
ridesFriends: {
paddingTop: 60,
alignItems: 'center',
flexDirection: 'row',
justifyContent: 'space-evenly',
// marginLeft: 2,
// marginRight: 3,
width: '100%',
},
numbersContainer: {
alignItems: 'center',
justifyContent: 'center',
flex: 1,
},
num1: {
fontSize: 30,
color: '#31C283',
fontWeight: 'bold',
borderRightWidth: 1,
borderColor: '#909090',
},
numbers: {
fontSize: 30,
color: '#31C283',
fontWeight: '900',
textShadowColor: '#000000',
textShadowRadius: 0.5,
},
verticleLine: {
height: '100%',
width: 1,
backgroundColor: '#E0E0E0',
//position: 'fixed',
},
As you have set the width of the parent to 100% you can easily wrap the number texts into views and set 50% each.当您将父级的宽度设置为 100% 时,您可以轻松地将数字文本包装到视图中并设置为 50%。 The code would be like below
代码如下
<View style={styles.ridesFriends}>
<View style={styles.numberWrap}>
<Text style={styles.numbers}>132</Text>
</View>
<View style={styles.verticleLine}></View>
<View style={styles.numberWrap}>
<Text style={styles.numbers}>2</Text>
</View>
</View>
Styles Styles
ridesFriends: {
paddingTop: 60,
alignItems: 'center',
flexDirection: 'row',
// marginLeft: 2,
// marginRight: 3,
width: '100%',
},
numbers: {
fontSize: 30,
color: '#31C283',
fontWeight: 'bold',
},
verticleLine: {
height: '100%',
width: 1,
backgroundColor: '#909090',
},
numberWrap: {
width: '50%',
alignItems: 'center',
},
You can add a View
component outside your text and style it with flex: 1
to equal separating space for your Text
component.您可以在文本之外添加一个
View
组件,并使用flex: 1
设置它的样式,以等于您的Text
组件的分隔空间。
Then you can add {alignItems: 'center',justifyContent: 'center'}
in your Text container view to archive your component然后你可以在你的文本容器视图中添加
{alignItems: 'center',justifyContent: 'center'}
来归档你的组件
Try mine:试试我的:
<View style={styles.ridesFriends}> <View style={styles.numbersContainer}> <Text style={styles.numbers}>132</Text> </View> <View style={styles.verticleLine}></View> <View style={styles.numbersContainer}> <Text style={styles.numbers}>2</Text> </View> </View> ridesFriends: { paddingTop: 70, alignItems: 'center', flexDirection: 'row', width: '100%', marginBottom: 20, }, numbersContainer: { alignItems: 'center', justifyContent: 'center', flex: 1, }, numbers: { fontSize: 30, color: '#31C283', fontWeight: 'bold', }, verticleLine: { height: '100%', width: 1, backgroundColor: '#909090', },
Hope that help:)希望有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.