美文网首页React Native
React Native WebView 高度和图片宽度自适应

React Native WebView 高度和图片宽度自适应

作者: Bana | 来源:发表于2018-09-03 16:45 被阅读125次

1.高度需要动态添加,我这边是加载html字符串

meta:为了让字体和div宽度适应手机
ResizeImages:为了图片自适应屏幕宽度,根据需要调整,这边是整体图片都适应了,没判断超出或者小于
onNavigationStateChange:通过WebView的方法,动态加载高度

 <View style={{ height: this.state.webViewHeight, width: ScreenWidth }}>
<WebView
            source={{html: 
              `<!DOCTYPE html>
              <html>
              <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
              <body>
              ${this.state.detailInfo.DetailHtml}
              <script>
              function ResizeImages(){
                var myimg;
                for(i=0;i <document.images.length;i++){
                  myimg = document.images[i];
                  myimg.width = ${ScreenWidth - 20};
                }
              }
              window.onload=function(){ 
                ResizeImages()
                window.location.hash = '#' + document.body.clientHeight;
                document.title = document.body.clientHeight;
              }
              </script></body></html>`
            }}
            style={{flex:1}}
            bounces={false}
            scrollEnabled={false}
            javaScriptEnabled={true}
            automaticallyAdjustContentInsets={true}
            contentInset={{top:0,left:0}}
            onNavigationStateChange={(title) => {
              console.log(title);
              if (title.title != undefined) {
                this.setState({
                  webViewHeight: (parseInt(title.title) + 20)
                })
              }
            }}
          />
        </View>

相关文章

网友评论

    本文标题:React Native WebView 高度和图片宽度自适应

    本文链接:https://www.haomeiwen.com/subject/dikiwftx.html