gpt4 book ai didi

reactjs - react native : how to center text?

转载 作者:行者123 更新时间:2023-12-03 12:52:14 28 4
gpt4 key购买 nike

如何在ReactNative中水平和垂直居中文本?

我在 rnplay.org 中有一个示例应用程序,其中 justifyContent="center"alignItems="center" 不起作用: https://rnplay.org/apps/AoxNKQ

文本应居中。为什么文本(黄色)和父容器之间的顶部有一个边距?

代码:

'use strict';

var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
Image,
View,
} = React;

var SampleApp = React.createClass({
render: function() {
return (
<View style={styles.container}>
<View style={styles.topBox}>
<Text style={styles.headline}>
lorem ipsum{'\n'}
ipsum lorem lorem
</Text>
</View>
<View style={styles.otherContainer}>
</View>
</View>
);
}
});

var styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
backgroundColor: 'red',
justifyContent: 'center',
alignItems: 'center',
},
topBox: {
flex: 1,
flexDirection: 'row',
backgroundColor: 'lightgray',
justifyContent: 'center',
alignItems: 'center',
},
headline: {
fontWeight: 'bold',
fontSize: 18,
marginTop: 0,
width: 200,
height: 80,
backgroundColor: 'yellow',
justifyContent: 'center',
alignItems: 'center',
},
otherContainer: {
flex: 4,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'green',
},
});

AppRegistry.registerComponent('SampleApp', () => SampleApp);

module.exports = SampleApp;

最佳答案

headline'样式中删除heightjustifyContentalignItems。它将垂直居中文本。添加 textAlign: 'center' 它将水平居中文本。

  headline: {
textAlign: 'center', // <-- the magic
fontWeight: 'bold',
fontSize: 18,
marginTop: 0,
width: 200,
backgroundColor: 'yellow',
}

关于reactjs - react native : how to center text?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37571418/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com