【问题标题】:Text of View doesn't align vertically in React Native视图文本在 React Native 中没有垂直对齐
【发布时间】: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。你试过alignSelfjustifyContent 吗?
  • 对不起,我发布了错误的代码。现在您可以看到更新的代码。是的,我已经尝试过使用 justifyContent 和 alignItems。
  • 您是否调试过Text 父级 (View) 是否使用了所有屏幕高度?

标签: css react-native flexbox


【解决方案1】:

请试试这个。

class SplashScreen extends Component {
render() {
    return (
        <View styles={{flex:1, backgroundColor: '#954876', justifyContent:'center', alignItems:'center'}}>
            <Text style={styles.splashScreenText}>Hello World!</Text>
        </View>
    );}}

【讨论】:

    猜你喜欢
    • 2020-05-08
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 2018-12-26
    • 2021-05-01
    • 2022-01-04
    • 1970-01-01
    • 2019-01-31
    相关资源
    最近更新 更多