alignItems: 'flex-end'无法正常工作(它不能覆盖整个页面)-React Native

3
在我的主页上,我有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',则此问题将消失,但我不能这样做!

有任何想法吗?


{btsdaf} - Asons
{btsdaf} - Asons
{btsdaf} - The Monster
{btsdaf} - The Monster
1
@Michael_B 是的,这是同样的问题。谢谢你提到它。 - The Monster
1
{btsdaf} - Asons
1个回答

2
你需要像这样做才能使其正常工作,其中<Card>元素是最外层的flex父级容器。
请注意添加了alignContent: 'flex-end',这在flex项目换行时是必需的。
<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,    
}

{btsdaf} - The Monster
{btsdaf} - Asons
{btsdaf} - The Monster
{btsdaf} - Asons
1
@TheMonster 并添加 alignContent: 'flex-end' - Asons
是的,我刚刚尝试了alignContent,它起作用了。谢谢。 - The Monster

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接