Responsive website show on Web view out of mobile

2020-06-01 06:25发布

I am using webview to load the responsive website but it shows the width outside to the mobile size.

I open the link in a mobile Chrome browser and it looks good...

I mean to say the width does not fit to screen size of mobile; it shows out of width.

I am also using these... for <viewport> tag for controlling its presentation.

    WebSettings settings = webview.getSettings();
    settings.setJavaScriptEnabled(true);

    settings.setUseWideViewPort(true);
    settings.setLoadWithOverviewMode(true); 

Also I am using setInitialScale for webview render properly.

    webview.setInitialScale((int) 1.0);

but I am getting the website load not responsive as according to size... it shows the width is outside to screen size... which comes with scroll

Application works fine in Lollipop, Marshmallow etc. But does not work in Jellybean etc. But the Link works well with latest chrome in all devices but does not work in webview.

I also set this....

 <meta name="viewport" content="width=device-width, initial-scale=1">

Screenshot

enter image description here

7条回答
我命由我不由天
2楼-- · 2020-06-01 07:21

May be you can achieve with onPageFinished() method

@Override
public void onPageFinished(android.webkit.WebView view, String url)
{
    super.onPageFinished(view, url);

    WindowManager manager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);

    DisplayMetrics metrics = new DisplayMetrics();
    manager.getDefaultDisplay().getMetrics(metrics);

    metrics.widthPixels /= metrics.density;

    wb.loadUrl("javascript:document.body.style.zoom = "+String.valueOf(scale)+";");

OR//
    wb.loadUrl("javascript:var scale = " + metrics.widthPixels + " / document.body.scrollWidth; document.body.style.zoom = scale;");
}

it worked fine with me time ago. Hope this helps...

查看更多
登录 后发表回答