private WebView webview=null; @SuppressLint("SetJavaScriptEnabled") private void initWebView(String html){ if(webview==null){ webview = new WebView(this); webview.setWebViewClient(new MyWebViewClient()); WebSettings webSettings = webview.getSettings(); if (Build.VERSION.SDK_INT >= 21) { webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); } // 设置支持javascript脚本 webSettings.setJavaScriptEnabled(true); // 设置此属性,可任意比例缩放 webSettings.setUseWideViewPort(true); // 设置不出现缩放工具 webSettings.setBuiltInZoomControls(false); // 设置不可以缩放 webSettings.setSupportZoom(false); webSettings.setDisplayZoomControls(false); //自适应屏幕 webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL); // 自适应 屏幕大小界面 webSettings.setLoadWithOverviewMode(true); LinearLayout contLayout = (LinearLayout)contentView.findViewById(R.id.goods_cont); contLayout.addView(webview); } Logger.e(html); html="<html><head><meta charset=\"UTF-8\"><style type=\"text/css\">html,body{padding:0px;margin:0px;} p{margin:0px;}</style></head><body>"+html+"</body></html>"; webview.loadDataWithBaseURL(null,html, "text/html", "utf-8", null); } private class MyWebViewClient extends WebViewClient { @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); // 重新测量 view.measure(w, h); } }
webview高度自适应图文详情
最新推荐文章于 2023-08-09 21:17:37 发布