如何在本机反应中获取元素的宽度?

2024-05-01

如何获取 React Native 中元素的宽度,例如 View ? 由于React Native中没有宽度的百分比使用,如何获取元素或父元素的宽度?


您可以致电onLayout测量元素的事件:

measureView(event) {
  console.log('event properties: ', event);
  console.log('width: ', event.nativeEvent.layout.width)
}

<View onLayout={(event) => this.measureView(event)}>

至于百分比宽度和高度,您可以获取窗口宽度和高度并像这样使用它们:

const {
    ...
    Dimensions
} = React

const width = Dimensions.get('window').width
const height = Dimensions.get('window').height

// 80% width
width: width * .8,

// 25% height
height: height / 4,
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在本机反应中获取元素的宽度? 的相关文章

随机推荐