React Native: Height vs flex

北城余情 提交于 2019-12-13 18:00:59

问题


While setting height of multiple components in a view, is there any difference between setting the height in terms of flex or height?

For eg,

1) <View>
       <View style={{flex:1}}>
       </View>
       <View style={{flex:3}}>
       </View>
   </View

2) var windowSize = Dimensions.get('window');
   <View>
       <View style={{height:windowSize.height/4}}>
       </View>
       <View style={{height:windowSize.height*3/4}}>
       </View>
   </View>

回答1:


Yes, when you rotate a device flex is responsive where as window isn't.

For example if the device dimension is 1280 x 720, it would result in the following:

Flex: portrait mode height = 1280. Landscape mode height = 720

Window: portrait mode height = 1280. Landscape mode height = 1280

Window retrieves your dimensions once when the component mounts. Yes you can manually add detectors for window when a screen rotates, but i don't see why you want to go through that much effort when flex is short and simple.




回答2:


Yes. For flex the size reference is its parent views layout size, whereas if you use window dimensions , it takes the whole window size as the reference. Try setting some height and width in 1) and see.



来源:https://stackoverflow.com/questions/41400775/react-native-height-vs-flex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!