通过删除 flex: 1,您将只看到视图的确切高度。
<ScrollView contentContainerStyle={{ backgroundColor: '#00ff00',
flexDirection: 'column', justifyContent: 'space-between' }}>
<View style={{ width: 100, height: 100, backgroundColor:'#b0e0e6' }} />
<View style={{ width: 100, height: 100, backgroundColor:'#87ceeb'}} />
<View style={{ width: 100, height: 100, backgroundColor:'#4682b4'}} />
</ScrollView>
![no flex](https://istack.dev59.com/FMX4Qm.webp)
如果你设置了flex: 1或者flexGrow: 1,ScrollView会将最小高度设置为屏幕的高度。
<ScrollView contentContainerStyle={{ flex: 1, backgroundColor: '#00ff00',
flexDirection: 'column', justifyContent: 'space-between' }}>
<View style={{ width: 100, height: 100, backgroundColor:'#b0e0e6' }} />
<View style={{ width: 100, height: 100, backgroundColor:'#87ceeb'}} />
<View style={{ width: 100, height: 100, backgroundColor:'#4682b4'}} />
</ScrollView>
![outer flex](https://istack.dev59.com/rmRd0m.webp)
如果视图的累积高度大于此值,则整个视图将超出屏幕高度。在这种情况下,flexGrow: 1(部分滚动显示)将允许您滚动到内容,但flex: 1会截断它。
<ScrollView contentContainerStyle={{ flexGrow: 1, backgroundColor: '#00ff00',
flexDirection: 'column', justifyContent: 'space-between'}}>
<View style={{ width: 100, height: 700, backgroundColor:'#b0e0e6' }} />
<View style={{ width: 100, height: 100, backgroundColor:'#87ceeb'}} />
<View style={{ width: 100, height: 100, backgroundColor:'#4682b4'}} />
</ScrollView>
![scrolled flexGrow](https://istack.dev59.com/0eKVHm.webp)
从那里开始,您可以在每个视图上设置flex以权衡空白空间将由视图填充的方式。例如,如果您在顶部两个视图上都设置了flex:1,并在底部视图上设置了flex:2,则在考虑内容高度后,底部视图将获得总高度的1/2,而顶部两个视图将各获得总高度的1/4。如下所示:
<ScrollView contentContainerStyle={{ flexGrow: 1, backgroundColor: '#00ff00',
flexDirection: 'column', justifyContent: 'space-between'}}>
<View style={{ flex: 1, width: 100, height: 100, backgroundColor:'#b0e0e6' }} />
<View style={{ flex: 1, width: 100, height: 100, backgroundColor:'#87ceeb'}} />
<View style={{ flex: 2, width: 100, height: 100, backgroundColor:'#4682b4'}} />
</ScrollView>
![inner flex](https://istack.dev59.com/JNte0m.webp)
ScrollView文档:https://facebook.github.io/react-native/docs/scrollview.html