【问题标题】:Center an item, then have an item to the left - Flexbox React Native居中一个项目,然后有一个项目在左边 - Flexbox React Native
【发布时间】:2016-01-12 21:01:00
【问题描述】:

在 React Native 中使用 flexbox 我在尝试执行以下看似简单的任务时遇到了很多麻烦:

我在一个容器中有两个项目。我想要一个居中,另一个直接位于它的左侧。

【问题讨论】:

    标签: flexbox react-native


    【解决方案1】:

    你可以改变两个外部组件的flex size属性,只要它们相同,中间的item就会居中。

    注意第一个组件的 justifyContent:'flex-end' 属性。这种样式使项目直接位于中间项目的左侧。

      <View style={styles.container}>
        <View style={styles.component1}>
            <Text>Right</Text>
        </View>
        <View style={styles.component2}>
            <Text style={{ textAlign:'center' }}>I am the Center Component</Text>  
        </View>
        <View style={styles.component3}></View>
      </View>
    
      container: {
        flexDirection:'row',
        flex:1,
        marginTop:60
      },
      component1: {
        flex:1,
        flexDirection: 'row',
        justifyContent: 'flex-end',
        height:80
      },
      component2: {
        flex:1,
        height:80,
        backgroundColor: 'red',
        justifyContent:'center'
      },
      component3: {
        flex:1,
      }
    

    另外,整个代码如下:

    'use strict';
    
    var React = require('react-native');
    var {
      AppRegistry,
      StyleSheet,
      Text,
      View,
    } = React;
    
    var SampleApp = React.createClass({
      render: function() {
        return (
          <View style={styles.container}>
            <View style={styles.component1}>
                        <Text>Right</Text>
            </View>
            <View style={styles.component2}>
                <Text style={{ textAlign:'center' }}>I am the Center Component</Text>  
            </View>
            <View style={styles.component3}></View>
          </View>
        );
      }
    });
    
    var styles = StyleSheet.create({
      container: {
        flexDirection:'row',
        flex:1,
        marginTop:60
      },
      component1: {
        flex:1,
        flexDirection: 'row',
        justifyContent: 'flex-end',
        height:80
      },
      component2: {
        flex:1,
        height:80,
        backgroundColor: 'red',
        justifyContent:'center'
      },
      component3: {
        flex:1,
      }
    });
    
    AppRegistry.registerComponent('SampleApp', () => SampleApp);
    

    【讨论】:

    • 我希望人们停止使用这些 https://rnplay.org/apps/phsgpw 链接,因为它们永远不会起作用
    猜你喜欢
    • 2020-01-25
    • 2017-07-01
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    • 2017-12-11
    • 2022-01-08
    • 1970-01-01
    相关资源
    最近更新 更多