reactjs – ReactNative:如何居中文本?

前端之家收集整理的这篇文章主要介绍了reactjs – ReactNative:如何居中文本?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在水平和垂直中将ReactNative中的文本居中?

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

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

码:

  1. 'use strict';
  2.  
  3. var React = require('react-native');
  4. var {
  5. AppRegistry,StyleSheet,Text,Image,View,} = React;
  6.  
  7. var SampleApp = React.createClass({
  8. render: function() {
  9. return (
  10. <View style={styles.container}>
  11. <View style={styles.topBox}>
  12. <Text style={styles.headline}>lorem ipsum{'\n'}ipsum lorem lorem</Text>
  13.  
  14. </View>
  15. <View style={styles.otherContainer}>
  16. </View>
  17. </View>
  18. );
  19. }
  20. });
  21.  
  22. var styles = StyleSheet.create({
  23.  
  24. container: {
  25. flex: 1,flexDirection: 'column',backgroundColor: 'red',justifyContent: 'center',alignItems: 'center',},topBox: {
  26. flex: 1,flexDirection: 'row',backgroundColor: 'lightgray',headline: {
  27. fontWeight: 'bold',fontSize: 18,marginTop: 0,width: 200,height: 80,backgroundColor: 'yellow',otherContainer: {
  28. flex: 4,backgroundColor: 'green',});
  29.  
  30. AppRegistry.registerComponent('SampleApp',() => SampleApp);
  31.  
  32. module.exports = SampleApp;

解决方法

标题’样式删除高度,justifyContent和alignItems.它将垂直居中文本.添加textAlign:’center’,它将水平居中文本.
  1. headline: {
  2. textAlign: 'center',// <-- the magic
  3. fontWeight: 'bold',}

猜你在找的Flex相关文章