React Native中Text背景色和垂直居中显示

1.Text设置背景色,圆角丢失。

        <View style={{ height: 64, width: 64,borderRadius: 12,justifyContent:'center' }}>
          <Text style={{ flex: 1 ,color:'#fff',fontSize:24,textAlign:'center',backgroundColor:'#ff0000'}}>1</Text>
        </View>
WX20181126-113058@2x.png

解决办法:背景色设置在View的styles属性中。

<View style={{ height: 64, width: 64,borderRadius: 12,justifyContent:'center',backgroundColor:'#ff0000'}}>
          <Text style={{ flex: 1 ,color:'#fff',fontSize:24,textAlign:'center'}}>1</Text>
        </View>
WX20181126-113259@2x.png

2.垂直居中:给Text在外层包一层View或添加lineHeight。

添加View:

        <View style={{ height: 64, width: 64,borderRadius: 12,backgroundColor:'#ff0000',justifyContent:'center'}}>
          <Text style={{color:'#fff',fontSize:24,textAlign:'center'}}>1</Text>
        </View>

添加lineHeight:

<Text style={{width:64,height:64,backgroundColor:'#ff0000',color:'#fff',fontSize:24,textAlign:'center',lineHeight:64}}>1</Text>
WX20181126-114725@2x.png

badgeView:

        <View style={{ height: 48, width: 48,borderRadius: 24,backgroundColor:'#ff0000',justifyContent:'center'}}>
          <Text style={{color:'#fff',fontSize:42,alignSelf:'center'}}>1</Text>
        </View>
WX20181126-145353@2x.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容