如何将内容与Flexbox的中心对齐

时间:2019-11-05 02:35:55

标签: css react-native flexbox native-base

我有一个项目要与中间对齐,一个要与底部对齐。

enter image description here

我希望徽标图像位于中间,但我不知道该怎么做。

import React, { Component } from 'react';
import { Container, Content, Text, Button, View } from 'native-base';
import { StyleSheet, Image } from 'react-native'

export default class WelcomeScreen extends Component {

render() {
    return (
        <Container>
            <Content contentContainerStyle={styles.container}>
                <View style={styles.imgContainer}>
                    <Image
                        source={require('../../assets/images/logos/logo.png')}
                    />
                </View>
                <View style={styles.btnContainer}>
                    <Button block primary onPress={() => this.props.navigation.navigate('Signin')} rounded>
                        <Text>Sign in</Text>
                    </Button>
                    <Button block light style={{ marginTop: 15 }} onPress={() => this.props.navigation.navigate('Signup')} rounded>
                        <Text>Sign up</Text>
                    </Button>
                </View>
            </Content>
        </Container>
    );
}
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center'
    },
    imgContainer: {
        flex: 1,
    },
    btnContainer: {
        width: 300,
    }
})

3 个答案:

答案 0 :(得分:2)

尝试一下

imgContainer: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center'
}

答案 1 :(得分:0)

这是仅HTML CSS的示例,您可以轻松进行调整以解决问题。

body {
  margin: 0;
}
#main {
  display: flex;
  flex-direction: column;
  height: 100vh;
  justify-content: space-between;
}
.container {
  align-items: middle;
  margin: 0 auto;
  align-self: middle;
  width: 300px;
  height: 80px;
  background-color: steelblue;
  vertical-align: afar;
}
<div id="main">
  <div class="container">
    abc
  </div>
  <div class="container">
    xyz
  </div>
</div>

诀窍在于以下CSS属性:

  • flex-direction: column
  • justify-content: space-between

答案 2 :(得分:0)

如果您希望图像除按钮高度以外的位置在屏幕中心对齐,可以使用以下代码,并将ImageContainer中的视图替换为Image

render() {
    return (
        <Container>           
             <View
             style={styles.imgContainer}>
             <View  style={{backgroundColor:'red',height:50,width:50}} />
             </View>

            <Content contentContainerStyle={styles.container}>

                <View style={styles.btnContainer}>
                    <Button block primary onPress={() => this.props.navigation.navigate('Signin')} rounded>
                        <Text>Sign in</Text>
                    </Button>
                    <Button block light style={{ marginTop: 15 }} onPress={() => this.props.navigation.navigate('Signup')} rounded>
                        <Text>Sign up</Text>
                    </Button>
                </View>
            </Content>
        </Container>
    );
}
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        alignItems: 'center',
        justifyContent: 'flex-end'
    },
    imgContainer: {
        position:'absolute',
        justifyContent:'center',
        alignItems:'center',
        height:Dimensions.get('window').height-40,
        width:Dimensions.get('window').width,
        flex: 1,
    },
    btnContainer: {
        height:200,
        width: 300,
        alignItems:'center',
        justifyContent: 'flex-end',
        marginBottom:20
    }
})