React-Native:测量视图

ila*_*sas 12 view measure react-native

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

我试图遵循这个:https://github.com/facebook/react-native/issues/953但它只是不起作用...我只想获得视图的实际宽度.

这是我的代码:

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>
    );
  }
}); 
Run Code Online (Sandbox Code Playgroud)

相关的风格:

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, 
  },
Run Code Online (Sandbox Code Playgroud)

Ary*_*NYC 8

您想要衡量进度条onLayout.

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>
    );
  }
});
Run Code Online (Sandbox Code Playgroud)

  • 这是目前最好的解决方案.我正在使用React Native 0.50.4并且不推荐使用字符串`ref`.此外,`View`的`measure`函数不会返回有效数据,除非`onLayout`被触发至少一次. (3认同)

nop*_*pon 7

要测量视图大小,根本不需要使用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)}} />
    );
}
Run Code Online (Sandbox Code Playgroud)

  • onLayout 在某些情况下不会调用,特别是在 Android 上。 (3认同)
  • 这是正确的@UmarFarooqAwan,但如果你设置 `collapsable={false}` 它总是会测量。在 Android 中,如果某些视图没有明显影响,则会被删除。 (2认同)

ila*_*sas 6

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

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

componentDidMount: function() {
  setTimeout(this.measureProgressBar);
},

measureProgressBar: function() {
  this.refs.progressBar.measure((a, b, width, height, px, py) =>
    this.setState({ progressMaxSize: width })
  );
}
Run Code Online (Sandbox Code Playgroud)

  • 在视图中传递给`onLayout`的回调函数中调用`measure`可能更有效. (3认同)
  • 你需要绑定measureProgressBar,this.measureProgressBar.bind(this) (2认同)