真正解决android ScrollView 嵌套WebView腾讯x5浏览器 导致底部大量空白

下面是x5浏览器的时候,一定写死高度,不要warpcontent,如果是系统webview,没测试,因为系统webview没问题。。。。但是测量法确实可以用,因为我写死高度,webview可以正常显示滑动,底部没有空白,所以写死高度,他的内容高度测量是没有问题。

第一步、在布局xml文件中 写死webview的高度,比如600dp 如果是linelayout父布局,也可以 android:layout_width=“match_parent”
android:layout_height=“0dp”
android:layout_weight=“1”
反正就是不能用warpcontent
第二步 .监听加载完成
方法1、重新测量

 			 override fun onPageFinished(p0: com.tencent.smtt.sdk.WebView?, p1: String?) {
            super.onPageFinished(p0, p1)
            dismissLoading()
            Log.i("lmj","contentHeight:"+)
            webView.measure(0, 0)//重新测量
            val measuredHeight = webView.measuredHeight//获取测量后的高度
            var wlp = webView.layoutParams as LinearLayout.LayoutParams
            wlp.height = measuredHeight
            webView.layoutParams = wlp//进行设置
         
            Log.i("lmj","measuredHeight:"+measuredHeight)

        }

方法2、不用测量,如果 使用x5浏览器 ,加载完成以后,getContentHeight()直接获得内容高度,注意这个时候单位是dp,转为px,重新设置布局就ok

webView.setWebViewClient(object : com.tencent.smtt.sdk.WebViewClient() {
        override fun onPageStarted(
            p0: com.tencent.smtt.sdk.WebView?,
            p1: String?,
            p2: Bitmap?
        ) {
            super.onPageStarted(p0, p1, p2)

        }
        override fun onPageFinished(p0: com.tencent.smtt.sdk.WebView?, p1: String?) {
            super.onPageFinished(p0, p1)
         

            try {
                var wlp = webView.layoutParams as LinearLayout.LayoutParams
                wlp.height = CommonUtil.dip2px(this@WebZiXunInfoActivity,webView.contentHeight)
                webView.layoutParams = wlp

                Log.i("lmj","measuredHeight:"+   wlp.height)
            } catch (e: Exception) {
            }

        }

        override fun shouldOverrideUrlLoading(
            p0: com.tencent.smtt.sdk.WebView?,
            request: com.tencent.smtt.export.external.interfaces.WebResourceRequest?
        ): Boolean {
            return false;
        }

    })
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值