如何将ReactNative中的文本水平和垂直居中
我在rnplay.org中有一个示例应用程序,其中justifyContent=“center”和alignItems=“center”不起作用:
https://rnplay.org/apps/AoxNKQ
文本应该居中。
为什么文本(黄色)和父容器之间的顶部有边距
代码:
'use strict';
var React=require('React-native');
变量{
评估学,
样式表,
文本
形象
看法
}=反应;
var SampleApp=React.createClass({
render:function(){
返回(
<;视图样式={styles.container}>;
<;视图样式={styles.topBox}>;
<;Text style={style.headline}>;lorem ipsum{'\n'}ipsum-lorem-lorem<;/Text>;
<;/View>;
<;视图样式={styles.otherContainer}>;
<;/View>;
<;/View>;
);
}
});
var styles=StyleSheet.create({
容器:{
弹性:1,
flexDirection:'列',
背景颜色:“红色”,
为内容辩护:“中心”,
对齐项目:“居中”,
},
机顶盒:{
弹性:1,
flexDirection:'行',
背景颜色:“浅灰色”,
为内容辩护:“中心”,
对齐项目:“居中”,
},
标题:{
fontWeight:'粗体',
尺码:18,
玛金托普:0,
宽度:200,
身高:80,
背景颜色:“黄色”,
为内容辩护:“中心”,
对齐项目:“居中”,
},
其他容器:{
弹性:4,
为内容辩护:“中心”,
对齐项目:“居中”,
背景颜色:“绿色”,
},
});
AppRegistry.registerComponent('SampleApp',()=>;SampleApp');
module.exports=SampleApp;
从标题“样式删除高度,证明内容和对齐项目。它将使文本垂直居中。添加textAlign:“居中”,它将使文本水平居中
标题:{
textAlign:‘居中’,//<;——魔法
fontWeight:'粗体',
尺码:18,
玛金托普:0,
宽度:200,
背景颜色:“黄色”,
}