How to open desktop site in webview in android

家住魔仙堡 提交于 2019-12-19 10:25:18

问题


I need to open desktop site in android webview for that i have tried as below but it is not working.

String newUA= "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/4.0";
mWebView.getSettings().setUserAgentString(newUA);

回答1:


This is the perfect solution:

 private static final String DESKTOP_USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36";
 private static final String MOBILE_USER_AGENT = "Mozilla/5.0 (Linux; U; Android 4.4; en-us; Nexus 4 Build/JOP24G) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30";

 //Choose Mobile/Desktop client.
 WebSettings settings = mWebView.getSettings();
 settings.setUserAgentString(DESKTOP_USER_AGENT);



回答2:


Try this:

    webView = (WebView)findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);

    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDisplayZoomControls(false);

    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.setScrollbarFadingEnabled(false);



回答3:


You can use setDesktopMode(true) from this WebView subclass or read how it's implemented. What is does is (a) set the user-agent not to include the words "mobile" or "Android" and (b) set the viewport to a larger width.




回答4:


This is working for me: refer to this as mentioned by @saurabh gutpa as well

public void setDesktopMode(final boolean enabled) {
    final WebSettings webSettings = webview.getSettings();

    final String newUserAgent;

    if (enabled) {
        newUserAgent = webSettings.getUserAgentString().replace("Mobile", "eliboM").replace("Android", "diordnA");
    }
    else {
        newUserAgent = webSettings.getUserAgentString().replace("eliboM", "Mobile").replace("diordnA", "Android");
    }

    webSettings.setUserAgentString(newUserAgent);
    webSettings.setUseWideViewPort(enabled);
    webSettings.setLoadWithOverviewMode(enabled);
    webSettings.setSupportZoom(enabled);
    webSettings.setBuiltInZoomControls(enabled);
}


来源:https://stackoverflow.com/questions/42931716/how-to-open-desktop-site-in-webview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!