在项目中可能会遇到这样的需求,可以使用onlayout获取高度,然后设置上去。
先在需要获取高度的地方绑定一个函数,
<View style={styles.OilCon} onLayout={this.changeViewLayout.bind(this)}>
<Text>hello, I am Test.</Text>
</View>
<View ref={test=> {
this.test = test;
}}
style={{ height: 0, overflow: "hidden" }}
>
<Text>改变</Text>
</View>
函数内拿到高度,
changeViewLayout(event) {
this.Height = event.nativeEvent.layout.height;
}
然后在需要的地方去修改高度,
this.test.setNativeProps({
style: {
height: this.Height
}
});