使webview中的内容自适应屏幕大小

来源:互联网 发布:太田宏介数据 编辑:程序博客网 时间:2024/06/04 20:10

private WebView pausePoster;


//设置暂停图片的webview初始化设置
        pausePoster.getSettings().setJavaScriptEnabled(true);
        pausePoster.getSettings().setUseWideViewPort(true);
        pausePoster.getSettings().setLoadWithOverviewMode(true);

        pausePoster.setVerticalScrollBarEnabled(false);
        pausePoster.setHorizontalScrollBarEnabled(false);


        file = new File(localAdUrl);
        String str = "<html>\n<head>\n  "
                + "<meta name=\"viewport\" content=\"width=device-width,user-scalable=no\"/>   "
                + "<style> html,body{background:transparent;margin:0;padding:0;}              "

                + "</style>\n    " 
                + "</head>\n" 
                + "<body>\n "
                + "<div style=\"width: 100%;height:100%;\">"
                + "<img name=\"gagImg\" style=\"width: 100%; height: 100%\" src=\"\"></div>"

                + "</body>\n " 
                + "<script type=\"text/javascript\">\n" 
                + "  window.onload=function(){var imgUrl= \"" 
                + file.getAbsolutePath()
                + "\"; " 
                + "document.gagImg.src = imgUrl;}" 
                + "     </script>\n" 
                + "</html>";
        String baseUrl = "file:///mnt" + Environment.getExternalStorageDirectory().getPath()
                + File.separator + Constant.PICSTOREDIR + File.separator + localAdUrl;
        pausePoster.loadDataWithBaseURL(baseUrl, str, "text/html", "utf-8", null);