首页 > 解决方案 > React Native:为什么不设置 flexGrow在一个里面工作?

问题描述

在我的 React Native 应用程序中,我有以下代码:

  <View style={{flex: 1}}>
    <View style={{backgroundColor: 'black', height: 50}}/>
    <ScrollView style={{borderWidth: 1, borderColor: 'red', flex: 1}}>
      <Text style={{
        borderWidth: 1,
        borderColor: 'blue',
        flexGrow: 1
      }}>aaa</Text>
      <Text style={{
        borderWidth: 1,
        borderColor: 'blue',
        flexGrow: 1
      }}>bbb</Text>
    </ScrollView>
  </View>

在此处输入图像描述

请注意,我有flexGrow: 1and aaas bbb <View>,但它们不会增长以填充垂直空间。如果我将 a 换成<ScrollView>a <View>,孩子们会填满它。为什么是这样?

标签: react-nativeflexboxreact-native-scrollview

解决方案


看看ScrollView这里的文档:https ://facebook.github.io/react-native/docs/scrollview.html#contentcontainerstyle

您缺少的关键是指定容器样式应该是什么。维护人员不会对您的期望做出任何假设,尤其是因为根据您过去的经验,它可能会让人感到困惑。你绝对可以得到你想要的工作。

这是添加到您的代码中的示例:https ://snack.expo.io/SkeLDj4EH


推荐阅读