gpt4 book ai didi

view - React-Native : measure a View

转载 作者:行者123 更新时间:2023-12-02 00:23:52 25 4
gpt4 key购买 nike

我正在尝试使用 ref 测量 View ,但返回的 width 为 0,尽管我看到屏幕上显示的 View (并且它是远不等于 0)。

我尝试遵循此操作:https://github.com/facebook/react-native/issues/953但它就是不起作用...我只想获取 View 的实际宽度。

这是我的代码:

var Time = React.createClass({
getInitialState: function() {
return({ progressMaxSize: 0 });
},
componentDidMount: function() {
this.measureProgressBar();
},
measureProgressBar: function() {
this.refs.progressBar.measure(this.setWidthProgressMaxSize);
},
setWidthProgressMaxSize: function(ox, oy, width, height, px, py) {
this.setState({ progressMaxSize: width });
},
render: function() {
return(
<View style={listViewStyle.time} ref="progressBar">
<View style={listViewStyle.progressBar} >
<View style={[listViewStyle.progressMax, { width: this.state.progressMaxSize }]}/>
</View>
</View>
);
}
});

关联的样式:

time: {
position: 'absolute',
bottom: 5,
left: 5,
right: 5,
backgroundColor: '#325436',
flexDirection: 'row',
},
progressBar: {
flex: 1,
backgroundColor: '#0000AA',
},
progressMax: {
position: 'absolute',
top: 0,
left: 0,
backgroundColor: '#000',
height: 30,
},

最佳答案

要测量 View 大小,根本不需要使用 ref。可以从onLayout传递的nativeEvent中获取。

measureView(event: Object) {
console.log(`*** event: ${JSON.stringify(event.nativeEvent)}`);
// you'll get something like this here:
// {"target":1105,"layout":{"y":0,"width":256,"x":32,"height":54.5}}
}

render() {
return (
<View onLayout={(event) => {this.measureView(event)}} />
);
}

关于view - React-Native : measure a View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29828971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com