android webview 加载富文本图片自适应
1、方法代码
fun imgReset(){ webView.loadUrl( "javascript:(function(){" + "var objs = document.getElementsByTagName('img'); " + "for(var i=0;i<objs.length;i++) " + "{" + "var img = objs[i]; " + " img.style.maxWidth = '100%'; img.style.height = 'auto'; " + "}" + "})()") }
2、使用时机
webView.webViewClient=object :WebViewClient(){ override fun onPageFinished(view: WebView?, url: String?) { super.onPageFinished(view, url) //webView.imgReset() imgReset() } }
3、生成扩展方法
fun WebView.imgReset() { this.loadUrl("""javascript:(function(){ |var objs = document.getElementsByTagName('img'); | for(var i=0;i<objs.length;i++){ | var img = objs[i]; | img.style.maxWidth = '100%'; | img.style.height = 'auto'; } | })()""".trimMargin()) }
android WebView 富文本神器