首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React-Native :测量视图

React-Native :测量视图
EN

Stack Overflow用户
提问于 2015-04-24 00:11:07
回答 3查看 44.6K关注 0票数 17

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

我试着遵循这个:https://github.com/facebook/react-native/issues/953,但它就是不能工作...我只想得到视图的实际宽度。

下面是我的代码:

代码语言:javascript
复制
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>
    );
  }
}); 

关联的样式:

代码语言:javascript
复制
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, 
  },
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-04-24 16:41:54

在晚上,一些人讨论了一个(hacky)解决方案,但它解决了我的问题,在这里找到它:https://github.com/facebook/react-native/issues/953

基本上,解决方案是使用setTimeout,一切都会神奇地工作:

代码语言:javascript
复制
componentDidMount: function() {
  setTimeout(this.measureProgressBar);
},

measureProgressBar: function() {
  this.refs.progressBar.measure((a, b, width, height, px, py) =>
    this.setState({ progressMaxSize: width })
  );
}
票数 8
EN

Stack Overflow用户

发布于 2018-06-04 23:50:53

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

代码语言:javascript
复制
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)}} />
    );
}
票数 14
EN

Stack Overflow用户

发布于 2017-08-23 22:53:24

您想要测量进度条onLayout

代码语言:javascript
复制
var Time = React.createClass({
  getInitialState: function() {
    return({ progressMaxSize: 0 }); 
  },
  measureProgressBar: function() {
    this.progressBar.measure(this.setWidthProgressMaxSize);
  },
  setWidthProgressMaxSize: function(ox, oy, width, height, px, py) {
    this.setState({ progressMaxSize: width });
  },
  render: function() {
    return(
      <View style={listViewStyle.time} ref={(c) => { this.progressBar = c; } onLayout={this.measureProgressBar}>
        <View style={listViewStyle.progressBar} >
          <View style={[listViewStyle.progressMax, { width: this.state.progressMaxSize }]}/>
        </View>
      </View>
    );
  }
});
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29828971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档