React Native提供了一个.measure(...)方法,该方法接受回调并使用组件的偏移量和宽度/高度调用它:
myComponent.measure( (fx, fy, width, height, px, py) => {
console.log('Component width is: ' + width)
console.log('Component height is: ' + height)
console.log('X offset to frame: ' + fx)
console.log('Y offset to frame: ' + fy)
console.log('X offset to page: ' + px)
console.log('Y offset to page: ' + py)
})
示例......
下面计算自定义组件呈现后的布局:
class MyComponent extends React.Component {
render() {
return { this.myComponent = view; }} />
}
componentDidMount() {
// Print component dimensions to console
this.myComponent.measure( (fx, fy, width, height, px, py) => {
console.log('Component widt