2016-05-16 22 views

回答

3

可以调用onLayout事件来衡量一个元素:至于百分比宽度和高度

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

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

,你可以得到的窗口的宽度和高度,并使用它们像这样:

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

请注意,在渲染组件后,会触发** onLayout事件。所以,如果你想chlld组件的百分比宽度而不重新渲染,我可以建议使用变量[这里](https://github.com/vitalets/react-native-extended-stylesheet#percents) – vitalets

+0

示例链接已损坏。请修复 – andrewgi