如何在本机反应中获取元素的宽度? [英] How to get width of an element in react native?
本文介绍了如何在本机反应中获取元素的宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 React Native 中获取元素的宽度,例如 View ?由于在 React Native 中没有使用百分比的宽度,如何获取元素或父元素的宽度?
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?
推荐答案
你可以调用 onLayout
事件来测量一个元素:
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,
这篇关于如何在本机反应中获取元素的宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文