我有以下布局,当数据大于屏幕时需要滚动:
图片:
代码:
export default () => (
<Container hasNavBar={false}>
<View style={{flex: 1}}>
<View style={Styles.container}>
<View style={{
flex: 1, backgroundColor: 'red',
alignItems: 'center',
justifyContent: 'center',
}}>
<Text>Area 1</Text>
</View>
<View style={{
flex: 2, backgroundColor: 'yellow',
alignItems: 'center',
justifyContent: 'center',
}}>
<Text>Area 2</Text>
</View>
</View>
</View>
</Container>
);
在添加scroll的react-native文档后,我需要使用ScrollView组件创建我的布局的包装器,但是当添加了scrollView组件时,我的布局破坏了:
图片:
代码:
export default () => (
<Container hasNavBar={false}>
<ScrollView style={{flex: 1}}>
<View style={Styles.container}>
<View style={{
flex: 1, backgroundColor: 'red',
alignItems: 'center',
justifyContent: 'center',
}}>
<Text>Area 1</Text>
</View>
<View style={{
flex: 2, backgroundColor: 'yellow',
alignItems: 'center',
justifyContent: 'center',
}}>
<Text>Area 2</Text>
</View>
</View>
</ScrollView>
</Container>
);
容器组件
<View style={flex: 1}>
{this.props.children}
<DropdownAlert
closeInterval={10000}
updateStatusBar={false}
ref={(ref) => this.dropdown = ref}
onClose={() => null}/>
</View>
我是如何解决的?
答案 0 :(得分:2)
在重新阅读问题并更好地理解您的完整代码后,很明显最快的解决方法是在区域1和区域2的视图上定义minHeight
。我们可以从窗口Dimensions
计算出来。这使您可以使用最少的内容获得33%/ 66%的比率,并根据需要扩展任何区域以及其他内容。
将它放在render()的顶部:
const { height } = Dimensions.get('window');
添加到区域1的样式
minHeight: height / 3
以及第2区的风格
minHeight: (height / 3) * 2