我正在实现一个动态高度的WebView.我发现这个解决方案就像iOS上的魅力一样,并且不适用于android.该解决方案使用WV中的JS将标题设置为内容高度的值.这是代码:
...
this.state = {webViewHeight: 0};
...
<WebView
source={{html: this.wrapWevViewHtml(this.state.content)}}
style={{width: Dimensions.get('window').width - 20, height: this.state.webViewHeight}}
scrollEnabled={false}
javaScriptEnabled={true}
injectedJavaScript="window.location.hash = 1;document.title = document.height;"
onNavigationStateChange={this.onWebViewNavigationStateChange.bind(this)}
/>
...
onWebViewNavigationStateChange(navState) {
// navState.title == height on iOS and html content on android
if (navState.title) {
this.setState({
webViewHeight: Number(navState.title)
});
}
}
...
Run Code Online (Sandbox Code Playgroud)
但在android上,内部标题的值onWebViewNavigationStateChange等于页面内容.
我究竟做错了什么?