Tôi đang cố gắng đo lường chế độ xem bằng cách sử dụng ref
, nhưng số width
được trả về là 0 mặc dù thực tế là tôi thấy chế độ xem được hiển thị trên màn hình (và nó cách xa bằng 0) .React-Native: đo Chế độ xem
Tôi đã cố gắng làm theo: https://github.com/facebook/react-native/issues/953 nhưng nó không hoạt động ... Tôi chỉ muốn có chiều rộng thực tế của Chế độ xem.
Đây là mã của tôi:
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>
);
}
});
Các phong cách liên quan:
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,
},
Bạn cũng có thể sử dụng requestAnimationFrame. –
Tôi nhận được 'undefined không phải là một đối tượng (đánh giá 'this.refs')'. Chuyện gì vậy? – fatuhoku
bạn cần phải ràng buộc measureProgressBar, this.measureProgressBar.bind (this) – meteors