Optimal WebView Settings for HTML5 Support?

心已入冬 提交于 2019-12-18 12:58:18

问题


I'm interested in determining what the optimal settings are for a WebView that is intended to show HTML5 content.

Currently I'm using :

mWebView.setFocusable(true);
mWebView.setFocusableInTouchMode(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.getSettings().setRenderPriority(RenderPriority.HIGH);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setDatabaseEnabled(true);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

With these settings, the WebView score 189 (w/ 1 bonus) on html5test.com. I am wondering if there are any settings that I should/could change to get further compatibility with HTML5. Of course, this list is an amalgamation of settings compiled over some months, so I'm also open to being told I'm doing something wrong. I do not have control over the html content to be displayed but am trying to support as broad a swath of HTML5 as possible.


回答1:


I would add:

    mWebView.setWebViewClient(new WebViewClient()); // tells page not to open links in android browser and instead open them in this webview



回答2:


Here is an (older) project concerned with optimal HTML5 settings:
http://code.google.com/p/html5webview/source/browse/trunk/HTML5WebView/src/org/itri/html5webview/HTML5WebView.java

FYI, normally I also set the database storage path for HTML5:

mWebView.getSettings().setDatabaseEnabled(true);
mWebView.getSettings().setDatabasePath("/data/data/" + Actvity.getPackageName() + "/databases/");



回答3:


You can also set like

try {
  WebSettings settings = view.getSettings();
      settings.setJavaScriptCanOpenWindowsAutomatically(true);
      settings.setSupportMultipleWindows(true);
      settings.setBuiltInZoomControls(true);
      settings.setJavaScriptEnabled(true);
      settings.setAppCacheEnabled(true);
      settings.setAppCacheMaxSize(10 * 1024 * 1024);
      settings.setAppCachePath("");
      settings.setDatabaseEnabled(true);
      settings.setDomStorageEnabled(true);
      settings.setGeolocationEnabled(true);
      settings.setSaveFormData(false);
      settings.setSavePassword(false);
      settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
      // Flash settings
      settings.setPluginState(WebSettings.PluginState.ON);

      // Geo location settings
      settings.setGeolocationEnabled(true);
      settings.setGeolocationDatabasePath("/data/data/selendroid");
    } catch (Exception e) {
      SelendroidLogger.error("Error configuring web view", e);
    }



回答4:


IMHO, the answer probably has much more to do than your Android version than specific settings.

Take a look at the "Tables" section of CanIUse.com:

  • http://caniuse.com/

... or MobileHTML5.org:

  • http://mobilehtml5.org/


来源:https://stackoverflow.com/questions/10097233/optimal-webview-settings-for-html5-support

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