【发布时间】:2020-02-21 20:37:32
【问题描述】:
我是 React Native 的新手。我试图了解 flexbox 在 RN 中的工作原理。鉴于我的 App 组件,我想将文本放在中心(垂直和水平)。
import React, {Component} from 'react';
import Colors from './src/assets/Colors';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
} from 'react-native';
class App extends Component {
constructor(props) {
super(props);
this.state = {
showSplashScreen: true,
};
}
render() {
let showSplashScreen = this.state.showSplashScreen;
return (
<View style={styles.appBackground}>
<SafeAreaView style={{flex:1}}>
{showSplashScreen && (<SplashScreen/>)}
</SafeAreaView>
</View>
);
}
}
class SplashScreen extends Component {
render() {
return (
<View styles={{flex:1, backgroundColor: '#954876'}}>
<Text style={styles.splashScreenText}>Hello World!</Text>
</View>
);
}
}
const styles = StyleSheet.create({
appBackground: {
height: '100%',
flex:1,
backgroundColor: Colors.customerGreen,
},
splashScreenText: {
color: Colors.white,
textAlign: 'center',
textAlignVertical: 'bottom',
},
});
export default App;
但我得到的是:
我完全不清楚为什么我的 flex 不能垂直拉伸,它应该是自动的,还是不是?
【问题讨论】:
-
AFAIK
textAlignVertical仅适用于 android,不适用于 iOS。你试过alignSelf和justifyContent吗? -
对不起,我发布了错误的代码。现在您可以看到更新的代码。是的,我已经尝试过使用 justifyContent 和 alignItems。
-
您是否调试过
Text父级 (View) 是否使用了所有屏幕高度?
标签: css react-native flexbox