React Native 之 Text居中显示
发布时间:2020-12-15 07:27:29 所属栏目:百科 来源:网络整理
导读:/** * Sample React Native App * https://github.com/facebook/react-native * @flow */import React,{ Component } from 'react';import { AppRegistry,StyleSheet,Image,Text,TouchableHighlight,View} from 'react-native';import ImageEqualEnlarge fro
/** * Sample React Native App * https://github.com/facebook/react-native * @flow */ import React,{ Component } from 'react'; import { AppRegistry,StyleSheet,Image,Text,TouchableHighlight,View } from 'react-native'; import ImageEqualEnlarge from './ImageEqualEnlarge.js' export default class ViewProject extends Component { render() { return ( <View style={styles.container}> <Text style={styles.textStyle}> happy </Text> <Text style={styles.textStyle}> 忧伤 </Text> </View> ); } } const styles = StyleSheet.create({ container: { flex:1,justifyContent: 'center',alignItems:'center',}, textStyle:{ height:100,width:200,fontSize:30,backgroundColor:'gray',textAlign:'center',//虽然样式中设置了 justifyContent: 'center',但无效 margin:5 }});AppRegistry.registerComponent('ViewProject',() => ViewProject);
Text 组件的样式键 textAlign 和 justifyContent 都设置为center,这样字符串应该垂直和水平都居中,但事实上,只会水平居中显示效果如图 正确的做法是 Text组件外边包一层View,且Text组件的样式除了fontSize键值其它样式键值都移到外层View的样式中 如下代码实现: /** * Sample React Native App * https://github.com/facebook/react-native * @flow */ import React,View } from 'react-native'; import ImageEqualEnlarge from './ImageEqualEnlarge.js' export default class ViewProject extends Component { render() { return ( <View style={styles.container}> <View style={styles.ViewForTextStyle}> <Text style={styles.newTextStyle}> happy </Text> </View> <View style={styles.ViewForTextStyle}> <Text style={styles.newTextStyle}> 忧伤 </Text> </View> </View> ); } } const styles = StyleSheet.create({ container: { flex:1,},newTextStyle:{ fontSize:30,textStyle:{ height:100,margin:5 },ViewForTextStyle:{ height:100,margin:5 } }); AppRegistry.registerComponent('ViewProject',() => ViewProject); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 在一行中添加大型XML文件中的换行符
- activerecord – Rails`NOT IN(null)`返回没有结果
- 【日常填坑】之ajax请求laravel的api接口
- ruby-on-rails – 如何使用FactoryGirl发送参数(而不是手动
- Caused by: org.xml.sax.SAXParseException; lineNumber: 3
- ruby – 为什么我的哈希打印成字符串?
- ruby-on-rails – 用Ruby on Rails播放Devise的用户
- applicationcontext.xml配置的问题
- c# – 实体框架:InvalidOperationException:无效的操作.连
- c# – 同一台Windows机器上的串行端口通信无法正常工作