下面是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;
}
})