如何在android中的webview中打开桌面站点

时间:2017-03-21 15:39:42

标签: android webview android-webview

我需要在android webview中打开桌面版网站,因为我尝试过如下操作,但它无法正常工作。

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);

5 个答案:

答案 0 :(得分:3)

这是一个完美的解决方案:

 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);

答案 1 :(得分:1)

试试这个:

    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);

答案 2 :(得分:1)

您可以使用this WebView subclass中的setDesktopMode(true)或阅读它implemented的方式。做的是(a)设置用户代理不包括单词" mobile"或" Android" (b)将视口设置为更大的宽度。

答案 3 :(得分:1)

对于移动用户代理

webview.getSettings().setUserAgentString("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");

对于桌面网站代理

browser.getSettings().setUserAgentString("Mozilla/5.0 (Linux; diordnA 7.1.1; suxeN 6 Build/N6F26U; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 eliboM Safari/537.36");

很多搜索都可以在Android webview上使用此代码。

答案 4 :(得分:0)

这对我有用:请同时参考@saurabh gutpa提到的this

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);
}