【问题标题】:alignItems: 'flex-end' doesn't work properly (it doesb't cover the whole page) - react native [duplicate]alignItems:'flex-end' 不能正常工作(它没有覆盖整个页面) - 反应原生 [重复]
【发布时间】:2018-04-03 23:46:45
【问题描述】:

所以在我的主页上,我有 4 个按钮,我用 flex 布置了这些按钮。我已经设置了父级的 flex: 1,这意味着它覆盖了整个页面(我已经用 backgroundColor 确定了这一点)。我的问题是,当我设置 alignItems: 'flex-end' 时,按钮只会向下移动一点,好像 flex 只覆盖了一半的页面。

这是我的代码标记:

<Card style={styles.cardStyle}>
      <CardSection style={styles.containerStyle}>
        <Button onPress={() => navigate("NewScreen")}>
          New
        </Button>
      </CardSection>

      <CardSection style={styles.containerStyle}>
        <Button onPress={() => navigate("SavedScreen")}>
          Saved
        </Button>
      </CardSection>

      <CardSection style={styles.containerStyle}>
        <Button onPress={() => navigate("ParametersScreen")}>
          Settings
        </Button>
      </CardSection>

      <CardSection style={styles.containerStyle}>
        <Button onPress={() => navigate("FMRScreen")}>
          FMR
        </Button>
      </CardSection>

    </Card>

卡片样式:

cardStyle: {
    flex: 1,
    flexDirection: 'row',
    flexWrap: 'wrap',
    justifyContent: 'space-around',
    alignItems: 'center',
    backgroundColor: '#0000ff',
}

CardSection 样式:

containerStyle: {
    borderBottomWidth: 1,
    padding: 5,
    backgroundColor: '#fff',
    borderColor: '#ddd',
    height: 150,
    width: 150,
    borderRadius: 20,
    marginTop: 10,
},

以及项目的样式:

textStyle: { 
    color: '#007aff',
    fontSize: 20,
    fontWeight: '600',
},
buttonStyle: {
    backgroundColor: 'rgba(255, 255, 255, 0)',
    borderWidth: 0,
    borderColor: '#007aff',
    flex: 1,
    flexDirection: 'column',
    justifyContent: 'center',
    alignItems: 'center',
},

这就是我得到的:

请注意,如果我删除 flexWrap: 'wrap',这个问题就会消失,但我不能这样做!

有什么想法吗?

【问题讨论】:

  • 还有弹性项目的 CSS ......?同时向我们展示标记
  • 好的,你需要在card上设置alignItems
  • @LGSon 到项目或父母?
  • @Michael_B 是的,这是同样的问题。感谢您提及。
  • @Michael_B 尽管我没有足够的代表,但我试图以骗子的身份关闭。对于初始标签:) ...当改变自己时,仍然不允许单手关闭,现在,我们知道这是同一个问题,你可以关闭它

标签: css react-native flexbox react-native-flexbox


【解决方案1】:

您需要执行类似的操作才能使其正常工作,其中 &lt;Card&gt; 元素是 flex 项的最外层 flex 父级。

注意添加的alignContent: 'flex-end',这是弹性项目包装时需要的

<Card style={styles.containerStyle}>

  <CardSection style={styles.sectionStyle}>
    <Button onPress={() => navigate("NewScreen")}>
      New
    </Button>
  </CardSection>

  <CardSection style={styles.sectionStyle}>
    <Button onPress={() => navigate("SavedScreen")}>
      Saved
    </Button>
  </CardSection>

  <CardSection style={styles.sectionStyle}>
    <Button onPress={() => navigate("ParametersScreen")}>
      Settings
    </Button>
  </CardSection>

  <CardSection style={styles.sectionStyle}>
    <Button onPress={() => navigate("FMRScreen")}>
      FMR
    </Button>
  </CardSection>

</Card>

containerStyle: {
    flex: 1,
    flexDirection: 'row',
    flexWrap: 'wrap',
    justifyContent: 'space-around',
    alignItems: 'flex-end',
    alignContent: 'flex-end',
    backgroundColor: '#0000ff',
}

sectionStyle: {
    borderBottomWidth: 1,
    padding: 5,
    backgroundColor: '#fff',
    borderColor: '#ddd',
    height: 150,
    width: 150,
    borderRadius: 20,
    marginTop: 10,    
}

【讨论】:

  • 我不明白。这不正是我的代码吗?
  • @TheMonster 不,您将containerStyle 添加到CardSection,它应该在Card 上...刚刚进行了更新,检查样式​​变量名称和应用位置标记
  • 我将它们写在不同的 .js 文件中,所以它们都具有相同的名称。我已经编辑了这个问题以便更好地澄清。我正在按照您在回答中所说的做,但它不起作用。
  • @TheMonster 更新后,请注意您使用的是alignItems: 'center', ...而不是flex-end
  • @TheMonster 并添加alignContent: 'flex-end'
猜你喜欢
  • 2021-09-06
  • 2014-07-12
  • 2018-01-06
  • 1970-01-01
  • 2019-01-09
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 2018-07-29
相关资源
最近更新 更多