How to get width of an element in react native?

后端 未结 1 1230
孤城傲影
孤城傲影 2021-02-20 01:56

How get width of an element in react native such as View ? As there is not percent usage for width in react native, how to get width of an element or parent element?

相关标签:
1条回答
  • 2021-02-20 02:44

    You can call the onLayout event to measure an element:

    measureView(event) {
      console.log('event properties: ', event);
      console.log('width: ', event.nativeEvent.layout.width)
    }
    
    <View onLayout={(event) => this.measureView(event)}>
    

    As far as percentage width and height, you can get the window width and height and use them like this:

    var {
       ...
       Dimensions
    } = React
    
    const width = Dimensions.get('window').width
    const height = Dimensions.get('window').height
    
    // 80% width
    width: width * .8,
    
    // 25% height
    height: height / 4,
    
    0 讨论(0)
提交回复
热议问题