1、Text内部的元素不再使用flexbox布局,而是采用文本布局。
textAlign属性:首字母为英文时,默认从左向右排列。 此时可以调整为center

2、Text居中显示的问题
在开发中,开发者时常需要将一串字符串显示在比字符串高度高很多的空间中,此时通常就需要水平与垂直方向都居中显示。`

 <TouchableHighlight activeOpacity={1} onPress={this.jumpToFlatList}>
          <View style={styles.box}>
            <Text style={styles.button}>FlatList长列表页面</Text>
          </View>
 </TouchableHighlight>


button: {
    height: 50,
    backgroundColor: '#000000',
    fontSize: 16,
   justifyContent: 'center',
   alignItems:'center'
  }

在flexbox样式中定义了justifyContent和alignItems。事实上只会水平居中显示
正确的做法是将text用View组件包起来。在view中设置居中,text中只设置除了fontSize和排列方式的样式

box: {
    height:50,
    backgroundColor: '#f3f3f3',
    marginBottom: 20,
    alignItems:'center',
    justifyContent: 'center'
  },
  button: {
    fontSize: 16,
    textAlign: 'center',
  },

3.ellipsizeMode取值为’head’,‘middle’,‘tail’,‘clip’。它决定了当Text组件无法全部显示时,字符串如何用省略号进行修饰。其分别为头部省略号,中间省略号,尾部省略号,无省略号。
ellipsizeMode需要与numberOfLines配合使用

<Text style={styles.title} ellipsizeMode='tail' numberOfLines={2}>{item.description}</Text>
<Text style={styles.jianjie} ellipsizeMode='tail' numberOfLines={1}>
{item.postDate} {item.runDistance} {item.biz}
</Text>
Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐