美文网首页
X5WebView(com.tencent.smtt.sdk.W

X5WebView(com.tencent.smtt.sdk.W

作者: _compass | 来源:发表于2020-07-06 23:12 被阅读0次

今天在某一项目里,发现一页面里内嵌X5WebView,会有奇怪的高频率闪烁问题。经过跟踪发现,是因为控件里设置了wrap_content。
经过几次修改,发现以下做法可改掉这个问题:

1. 在xml里预先给X5WebView设置一个高度,例:

       android:layout_width="match_parent"
       android:layout_height="150dp"

2.当页面加载完毕后,重新设置高度:

       webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                webView.loadUrl(s);
                return true;
            }

            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
                int height = webView.getContentHeight();
                if (height > 0) {
                    ViewGroup.LayoutParams params = webView.getLayoutParams();
                    params.height = DensityUtil.dp2px(Activity.this, height);;
                    webView.setLayoutParams(params);
                }
            }
      });

这样就不会闪烁了。

相关文章

网友评论

      本文标题:X5WebView(com.tencent.smtt.sdk.W

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