![](/img/trans.png)
[英]React-Native: Center two text components in a single View component
[英]React Native: Unable to center Text in View component
我無法在android和ios上將 React Native 的View
組件中的Text
組件居中。
正如您所看到的, +符號不在白色圓圈的中心。
這是我的組件:
<TouchableOpacity
style={styles.blueButton}
>
<View style={styles.addButton}>
<Text style={styles.plus}>+</Text>
</View>
</TouchableOpacity>
這是我的樣式表:
blueButton: {
height: 40,
width: 40,
borderRadius: 3,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#3498DB',
},
addButton: {
width: 15,
height: 15,
borderRadius: 30,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'white',
},
plus: {
color: '#3498DB',
fontSize: 20,
},
你可以試試我下面的代碼:
組件代碼:
<View style={styles.addButtonBlue}>
<View style={styles.addButton}>
<Text style={styles.plus}>+</Text>
</View>
</View>
樣式表代碼
addButtonBlue: {
width: 70,
height: 70,
borderRadius: 10,
justifyContent: 'center',
alignContent: 'center',
alignItems: 'center',
backgroundColor: '#3498db',
},
addButton: {
width: 30,
height: 30,
borderRadius: 30,
justifyContent: 'center',
alignContent: 'center',
alignItems: 'center',
backgroundColor: '#fff',
},
plus: {
color: '#3498DB',
},
android 中額外的字體填充可能是這里的罪魁禍首。 嘗試將includeFontPadding: false
添加到文本樣式。
更多信息在這里 - https://facebook.github.io/react-native/docs/text#style
includeFontPadding: bool (Android)
設置為 false 以刪除旨在為某些上升/下降空間騰出空間的額外字體填充。 對於某些字體,當垂直居中時,這種填充會使文本看起來略微錯位。 為了獲得最佳結果,還將 textAlignVertical 設置為居中。 默認為真。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.