加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读