React Native:用于 ScrollView 的 Flex 不起作用

pfu*_*t75 5 css ios flexbox react-native react-native-flexbox

我有一个简单的 View,它有两个孩子,另一个 View 和一个 ScrollView。ScrollView 应该比同级别的 View 高 5 倍,使用 flexbox。

<View style={{ flex: 1}}>
    <View style={{flex: 1, backgroundColor: 'powderblue'}}>
      <Text>Add new stuff</Text>
      <Button title="Browse Gallery" onPress={ openGallery } />
    </View>
    <ScrollView style={{flex: 5, backgroundColor: 'skyblue'}}>
          <Text style={{ fontSize: 100}}>Scroll me plz</Text>
          <Text style={{ fontSize: 100}}>Scroll me plz</Text>
    </ScrollView>
  </View>
Run Code Online (Sandbox Code Playgroud)
所以我简单地将 flex:1 和 flex:5 添加到子视图中,但在渲染视图中,它们都恰好适合屏幕的一半。看起来好像忽略了两个 flex 属性......

顺便说一句,使用第二个 View 而不是 ScrollView 效果很好!

任何想法如何使用 ScrollView 实现 1:5 的比例?

Rey*_*nes 8

ScrollView 是一个不继承flex. 您想要做的是将 ScrollView 包装在一个 View 中,这将创建您想要的弹性比率。

  <View style={{flex: 1}}>
    <View style={{flex: 1, backgroundColor: 'powderblue'}}>
      <Text>Add new stuff</Text>
      <Button title="Browse Gallery" onPress={ openGallery } />
    </View>
    <View style={{flex: 5}}>
      <ScrollView style={{backgroundColor: 'skyblue'}}>
            <Text style={{ fontSize: 100}}>Scroll me plz</Text>
            <Text style={{ fontSize: 100}}>Scroll me plz</Text>
      </ScrollView>
    </View>
  </View>
Run Code Online (Sandbox Code Playgroud)