Android WebView 适合屏幕内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25440045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:25:53  来源:igfitidea点击:

Android WebView fit content to screen

androidwebview

提问by Vierda Mila Nartila

I'm trying to fit webview content with screen but it display very ugly and show different results, please see below link for screen capture :

我正在尝试将 webview 内容与屏幕匹配,但它显示非常难看并显示不同的结果,请参阅下面的屏幕截图链接:

http://postimg.org/image/jy0g268p1/0294ca52/

http://postimg.org/image/jy0g268p1/0294ca52/

http://postimg.org/image/603z8qhab/e06b655e/

http://postimg.org/image/603z8qhab/e06b655e/

Please find below my code :

请在我的代码下面找到:

WebSettings settings = webView.getSettings();
    settings.setMinimumFontSize(30);
    settings.setLoadWithOverviewMode(true);
    settings.setUseWideViewPort(true);
    settings.setBuiltInZoomControls(true);
    settings.setSupportZoom(true); 

    webView.loadData(htmlContent,  "text/html", "UTF-8");
    webView.setInitialScale(1);
    webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

Kindly advise what should I do to fit content nicely? Really appreciate for any kind help.

请建议我应该怎么做才能很好地适应内容?真的很感激任何帮助。

采纳答案by MEGHA DOBARIYA

I have create my own method with set background color and font color also.

 WebSettings settings = desc.getSettings();
            settings.setMinimumFontSize(50);
            desc.getSettings().setJavaScriptEnabled(true);
            settings.setLoadWithOverviewMode(true);
            settings.setUseWideViewPort(true);
            settings.setBuiltInZoomControls(true);
            settings.setDisplayZoomControls(false);

            desc.setWebChromeClient(new WebChromeClient());
            String changeFontHtml = changedHeaderHtml(description);
            desc.setBackgroundColor(context.getResources().getColor(R.color.all_app_bg_color));
            desc.loadDataWithBaseURL(null, changeFontHtml,"text/html", "UTF-8", null);
            desc.setWebViewClient(new WebViewClient() {
                public void onPageFinished(WebView view, String url) {
                    view.loadUrl("javascript:document.body.style.setProperty(\"color\", \"white\");"
                    );
                }
            });


    public static String changedHeaderHtml(String htmlText) {

            String head = "<head><meta name=\"viewport\" content=\"width=device-width, user-scalable=yes\" /></head>";

            String closedTag = "</body></html>";
            String changeFontHtml = head + htmlText + closedTag;
            return changeFontHtml;
        }

回答by The Reptilian Army

This worked for me:

这对我有用:

webview.Settings.LoadWithOverviewMode = true;
webview.Settings.UseWideViewPort = true;

回答by Vierda Mila Nartila

I think I have found my own solution, I put here for someone who need it in future. I just create one method to change head of html :

我想我已经找到了自己的解决方案,我把它放在这里供将来需要它的人使用。我只是创建了一种方法来更改 html 的头部:

public static String changedHeaderHtml(String htmlText) {

        String head = "<head><meta name=\"viewport\" content=\"width=device-width, user-scalable=yes\" /></head>";

        String closedTag = "</body></html>";
        String changeFontHtml = head + htmlText + closedTag;
        return changeFontHtml;
    }

And I'm using it inside webview as follow :

我在 webview 中使用它如下:

public static void displayHtmlText(String htmlContent, String message,
        WebView webView,
        RelativeLayout videoLayout, LinearLayout standardLayout, LinearLayout webviewLayout){

    WebSettings settings = webView.getSettings();
    settings.setMinimumFontSize(18);
    settings.setLoadWithOverviewMode(true);
    settings.setUseWideViewPort(true);
    settings.setBuiltInZoomControls(true);
    settings.setDisplayZoomControls(false);

    webView.setWebChromeClient(new WebChromeClient());
    String changeFontHtml = Util.changedHeaderHtml(htmlContent);
    webView.loadDataWithBaseURL(null, changeFontHtml,
            "text/html", "UTF-8", null);

    webviewLayout.setVisibility(View.VISIBLE);
    standardLayout.setVisibility(View.GONE);
    videoLayout.setVisibility(View.GONE);
}

So my content in webview now is fit to device and can show nicely.

所以我在 webview 中的内容现在适合设备并且可以很好地显示。