Setting component height to 100% in react-native

后端 未结 7 1789
终归单人心
终归单人心 2021-01-31 14:29

I can give the height element of style numeric values such as 40 but these are required to be integers. How can I make my component to have a height of 100%

相关标签:
7条回答
  • 2021-01-31 14:50

    flex:1 should work for almost any case. However, remember that for ScrollView, it's contentContainerStyle that controls the height of view:

    WRONG

    const styles = StyleSheet.create({
      outer: {
        flex: 1,
      },
      inner: {
        flex: 1
      }
    });
    
    <ScrollView style={styles.outer}>
      <View style={styles.inner}>
      </View>
    </ScrollView>
    

    CORRECT

    const styles = StyleSheet.create({
      outer: {
        flex: 1,
      },
      inner: {
        flex: 1
      }
    });
    
    <ScrollView contentContainerStyle={styles.outer}>
      <View style={styles.inner}>
      </View>
    </ScrollView>
    
    
    0 讨论(0)
  • 2021-01-31 14:52

    Try this:

      <View style={{flex: 1}}>
        <View style={{flex: 1, backgroundColor: 'skyblue'}} />
      </View>
    

    You can have more help in react-native online documentation (https://facebook.github.io/react-native/docs/height-and-width).

    0 讨论(0)
  • 2021-01-31 14:54

    You can simply add height: '100%' into your item's stylesheet. it works for me

    0 讨论(0)
  • 2021-01-31 15:03

    check out the flexbox doc. in the stylesheet, use:

    flex:1,
    
    0 讨论(0)
  • 2021-01-31 15:03

    <View style={styles.container}> </View>

    const styles = StyleSheet.create({ container: { flex: 1 } })

    0 讨论(0)
  • 2021-01-31 15:04

    Grab the window height into a variable, then assign it as the height of the flex container you want to target :

    let ScreenHeight = Dimensions.get("window").height;
    

    In your styles :

    var Styles = StyleSheet.create({ ... height: ScreenHeight });
    

    Note that you have to import Dimensions before using it:

    import { ... Dimensions } from 'react-native'
    
    0 讨论(0)
提交回复
热议问题