【问题标题】:React Native, having problems with Flex BoxReact Native,Flex Box 有问题
【发布时间】:2018-06-28 15:00:43
【问题描述】:

我正在练习我的 React Native 编程,但我对 Flex 布局有一个问题,我似乎不太了解

我想让我的测试应用看起来像下面这张图片

Sample App

但最后,这就是我得到的。假设正确居中的“主页”文本中有一些错位,并且两个右侧图标上缺少间隙。

Sample Test App

我尝试在两个图标上添加填充,但没有成功。

这是我的代码:

import React from 'react';
import Icon from 'react-native-vector-icons/Feather';
import { StyleSheet, Text, View } from 'react-native';


export default class App extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <View style={styles.headerContainer}>
          <View style={styles.headerBrandingContainer}>
            <Text style={styles.headerBranding}>Brand</Text>
          </View>
          <View style={styles.headerRowContainer}>
            <Icon name="menu" size={30} color="white" />
            <Text style={styles.headerRowPage}>Home</Text>
            <View style={styles.headerRowIcons}>
              <Icon name="filter" size={30} color="white" />
              <Icon name="search" size={30} color="white" />
            </View>
          </View>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'column',

  },
  headerContainer: {
    backgroundColor: '#3498db',
    height: 180,
    justifyContent: 'center',
  },
  headerBrandingContainer: {
    marginTop: 50,
    alignItems: 'center',
  },
  headerBranding: {
    fontSize: 40,
    fontWeight: '400',
    letterSpacing: 1,
    color: '#fff',
  },
  headerRowContainer: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    margin: 10
  },
  headerRowIcons: {
    //backgroundColor: 'red',
    flexDirection: 'row',
    justifyContent: 'space-between',
  },
  headerRowPage: {
    // marginLeft:20,
    fontSize: 25,
    fontWeight: '500',
    color: '#fff',

  }
});

【问题讨论】:

  • 试试flex:1 换成headerRowIcons
  • @AravindS 在 headerRowIcons 上尝试了 flex:1,它会将所有内容一直推到左侧。

标签: javascript reactjs react-native flexbox


【解决方案1】:

要垂直对齐 headerContainer 的子项,您应该使用 alignItems 和以下代码:

headerContainer: {
    display: 'flex',
    backgroundColor: '#3498db',
    height: 180,
    justifyContent: 'center',
    alignItems: 'center'
}

了解 flexbox 的有用资源:https://css-tricks.com/snippets/css/a-guide-to-flexbox/

也删除 headerBrandingContainer 样式上的 marginTop。

编辑:

最后我觉得最好的办法是在组件树中进行一些修改,让headerRowContainer项都是flex元素为1。这样,标题总是居中(视图大小相同),我们可以现在管理按钮的位置而不影响其余部分。它非常适合我。

import React from 'react';
import Icon from 'react-native-vector-icons/Feather';
import { StyleSheet, Text, View } from 'react-native';

export default class App extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <View style={styles.headerContainer}>
          <View style={styles.headerBrandingContainer}>
            <Text style={styles.headerBranding}>Brand</Text>
          </View>
          <View style={styles.headerRowContainer}>
            <View style={styles.buttonsContainerLeft}>
              <Icon name="menu" size={30} color="white" />
            </View>
            <View style={styles.titleContainer}>
              <Text style={styles.headerRowPage}>Home</Text>
            </View>
            <View style={styles.headerRowIcons}>
              <Icon
                name="filter"
                size={30}
                color="white"
                style={styles.filterIcon}
              />
              <Icon name="search" size={30} color="white" />
            </View>
          </View>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'column'
  },
  headerContainer: {
    backgroundColor: '#3498db',
    height: 180,
    justifyContent: 'center'
  },
  headerBrandingContainer: {
    marginTop: 50,
    alignItems: 'center'
  },
  headerBranding: {
    fontSize: 40,
    fontWeight: '400',
    letterSpacing: 1,
    color: '#fff'
  },
  headerRowContainer: {
    flex: 1,
    flexDirection: 'row',
    alignItems: 'center',
    margin: 10
  },
  buttonsContainerLeft: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'flex-start'
  },
  titleContainer: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center'
  },
  headerRowIcons: {
    flex: 1,
    alignItems: 'center',
    flexDirection: 'row',
    justifyContent: 'flex-end'
  },
  headerRowPage: {
    fontSize: 25,
    fontWeight: '500',
    color: '#fff'
  },
  filterIcon: {
    marginRight: 20
  }
});

【讨论】:

  • 我尝试过使用 alignItem,但即使我在 justifyContent 中使用空格,所有东西都挤在 rowContainer 中
  • 并删除 headerBrandingContainer 样式中的 marginTop ?
  • 没有marginTop还是一样
  • 我试图在 codepen 上重现,我在过滤器图标上使用 margin-right 来添加间距和对齐项目:居中。行为似乎正确:codepen.io/Maestro31/pen/BVGPjB
  • 我尝试在 codepen 中遵循所有内容,但在 React Native 中结果完全不同。这就是我得到的 -> ibb.co/jbC3AT
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-14
  • 2018-10-19
  • 2016-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多