【问题标题】:Centre Buttons horizontally and vertically水平和垂直居中按钮
【发布时间】:2019-08-17 13:52:39
【问题描述】:

我是 React Native 的新手,我正在尝试将我创建的组件垂直和水平居中。如果我将中心样式代码放在整体视图中,它就可以工作,但这也会使 app.js 中心的所有内容。应该有一种方法只使我制作的组件居中。

我已尝试更改 flex 和 diff flexbox 选项。

import React, { Component } from 'react';
import {
    Platform,
    Animated,
    StyleSheet,
    Text,
    View,
    Button,
    Alert,
    TouchableHighlight,
} from 'react-native';
import Animater from './components/Animater';
import PresentationalComponent from './components/PresentationalComponent';

const AnimatedButton = Animated.createAnimatedComponent(TouchableHighlight);

export default class HelloWorldApp extends Component {

    constructor(props) {
        super(props);
        this.state = {
            listDataFromChild: null
        };
    }

    myCallback = (dataFromChild) => {
        this.setState({ listDataFromChild: dataFromChild });
    }

    render() {

      return (
        <View >     
          <Text style={styles.score}>
            Score: {this.state.listDataFromChild}
          </Text>
          <View style={styles.container}>
            <Animater  callbackFromParent={this.myCallback}/>     
          </View>
        </View>
        //<PresentationalComponent color = {[animatedStyle]} showAlert = {this.showAlert}/>
        );
    }
}


const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        position: 'absolute',
        paddingHorizontal: 10,

    },
    score: {
        fontSize: 30,
        alignItems: 'center',
        top: 100,

    },
});

我希望 Animator 组件位于中心,但它却保留在屏幕的左上角

【问题讨论】:

    标签: reactjs react-native


    【解决方案1】:

    要将居中的项目与位置对齐:'absolute',您必须添加以下样式

     {
        position: "absolute",
        top: 0,
        left: 0,
        right: 0,
        bottom: 0,
        justifyContent: "center",
        alignItems: "center"
      }
    

    另外请用 flex:1 制作主容器

    { flex: 1 }
    

    完整代码

    import React from "react";
    import { View, Text, StyleSheet } from "react-native";
    
    export default class Home1 extends React.Component {
      render() {
        return (
          <View style={styles.mainContainer}>
            <Text style={styles.score}>Score: 50</Text>
            <View style={styles.container}>
              <Text>Animator container</Text>
            </View>
          </View>
        );
      }
    }
    
    const styles = StyleSheet.create({
      mainContainer: { flex: 1 },
      container: {
        position: "absolute",
        top: 0,
        left: 0,
        right: 0,
        bottom: 0,
        justifyContent: "center",
        alignItems: "center"
      },
      score: {
        fontSize: 30,
        alignItems: "center",
        top: 100
      }
    });
    
    

    【讨论】:

    • 我知道有没有其他方法可以在没有绝对位置的情况下做到这一点?
    • 如果您不想使用 "absolute" ,这取决于视图有多少高度,如果 flex:1 则视图具有完整的高度和宽度以及屏幕中心的项目。如果视图没有 flex:1 那么视图将是宽度和高度内容的中心。您也可以在样式中使用“alignself”来使视图居中
    【解决方案2】:

    你的风格应该是,

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

    注意:使用flex 时,我们不需要position 属性。

    【讨论】:

    • 我尝试了这个但我收到一个错误:[ESLint] 'flex' in not defined (no-undef)
    • 尝试像显示一样计算 flex:'flex'
    • 同一行是否抛出错误?或者您在其他地方使用过 flex?检查是否使用了任何 flex。
    • 移除位置:absolute 并显示:flex。