WebView控件之WebSettings各种配置方法源码总结

本文转载自:http://teachcourse.cn/android-webview-websettings
WebSettings用于管理WebView状态配置,当WebView第一次被创建时,WebView包含着一个默认的配置,这些默认的配置将通过get方法返回,通过WebView中的getSettings方法获得一个WebSettings对象,如果一个WebView被销毁,在WebSettings中所有回调方法将抛出IllegalStateException异常。

  1. setSupportZoom(boolean support)
    设置WebView是否支持使用屏幕控件或手势进行缩放,默认是true,支持缩放。
    getSettings.setSupportZoom(false);
  2. setMediaPlaybackRequiresUserGesture(boolean require)
    设置WebView是否通过手势触发播放媒体,默认是true,需要手势触发。
    getSettings.setMediaPlaybackRequiresUserGesture(false);
  3. setBuiltInZoomControls(boolean enabled)
    设置WebView是否使用其内置的变焦机制,该机制集合屏幕缩放控件使用,默认是false,不使用内置变焦机制。
    getSettings.setBuiltInZoomControls(true);
  4. setDisplayZoomControls(boolean enabled)
    设置WebView使用内置缩放机制时,是否展现在屏幕缩放控件上,默认true,展现在控件上。
    getSettings.setDisplayZoomControls(false);
  5. setAllowFileAccess(boolean allow)
    设置在WebView内部是否允许访问文件,默认允许访问。
    getSettings.setAllowFileAccess(false);
  6. setAllowContentAccess(boolean allow)
    设置WebView是否使用其内置的变焦机制,该机制结合屏幕缩放控件使用,默认是false,不使用内置变焦机制。
    getSettings.setAllowContentAccess(false);
  7. setLoadWithOverviewMode(boolean overview)
    设置WebView是否使用预览模式加载界面。
    getSettings.setLoadWithOverviewMode(false);
  8. setSaveFormData(boolean save)
    设置WebView是否保存表单数据,默认true,保存数据。
    getSettings.setSaveFormData(false);
  9. setTextZoom(int textZoom)
    设置WebView中加载页面字体变焦百分比,默认100,整型数。
    getSettings.setTextZoom(100);
  10. setAcceptThirdPartyCookies(boolean accept)
    设置WebView访问第三方Cookies策略,参考CookieManager提供的方法:setShouldAcceptThirdPartyCookies。
    getSettings.setAcceptThirdPartyCookies(false);
  11. setUseWideViewPort(boolean use)
    设置WebView是否使用viewport,当该属性被设置为false时,加载页面的宽度总是适应WebView控件宽度;当被设置为true,当前页面包含viewport属性标签,在标签中指定宽度值生效,如果页面不包含viewport标签,无法提供一个宽度值,这个时候该方法将被使用。
    getSettings.setUseWideViewPort(false);
  12. setSupportMultipleWindows(boolean support)
    设置WebView是否支持多屏窗口,参考WebChromeClient#onCreateWindow,默认false,不支持。
    getSettings.setSupportMultipleWindows(true);
  13. setLayoutAlgorithm(LayoutAlgorithm l)
    设置WebView底层的布局算法,参考LayoutAlgorithm#NARROW_COLUMNS,将会重新生成WebView布局
    getSettings.setLayoutAlgorithm(LayoutAlgorithm l);
  14. setStandardFontFamily(String font)
    设置WebView标准字体库字体,默认字体“sans-serif”。
    getSettings.setStandardFontFamily("sans-serif");
  15. setFixedFontFamily(String font)
    设置WebView固定的字体库字体,默认“monospace”。
    getSettings.setFixedFontFamily("monospace");
  16. setSansSerifFontFamily(String font)
    设置WebView Sans SeriFontFamily字体库字体,默认“sans-serif”。
    getSettings.setSansSerifFontFamily("sans-serif");
  17. setSerifFontFamily(String font)
    设置WebView seri FontFamily字体库字体,默认“sans-serif”。
    getSettings.setSansSerifFontFamily("sans-serif");
  18. setCursiveFontFamily(String font)
    设置WebView字体库字体,默认“cursive”
    getSettings.setCursiveFontFamily("cursive");
  19. setFantasyFontFamily(String font)
    设置WebView字体库字体,默认“fantasy”。
    getSettings.setFantasyFontFamily("fantasy");
  20. setMinimumFontSize(int size)
    设置WebView字体最小值,默认值8,取值1到72
    getSettings.setMinimumFontSize(8);
  21. setMinimumLogicalFontSize(int size)
    设置WebView逻辑上最小字体值,默认值8,取值1到72
    getSettings.setMinimumLogicalFontSize(8);
  22. setDefaultFontSize(int size)
    设置WebView默认值字体值,默认值16,取值1到72
    getSettings.setDefaultFontSize(16);
  23. setDefaultFixedFontSize(int size)
    设置WebView默认固定的字体值,默认值16,取值1到72
    getSettings.setDefaultFixedFontSize(16);
  24. setLoadsImagesAutomatically(boolean flag)
    设置WebView是否加载图片资源,默认true,自动加载图片
    getSettings.setLoadsImagesAutomatically(false);
  25. setBlockNetworkImage(boolean flag)
    设置WebView是否以http、https方式访问从网络加载图片资源,默认false
    getSettings.setBlockNetworkImage(true);
  26. setBlockNetworkLoads(boolean flag)
    设置WebView是否从网络加载资源,Application需要设置访问网络权限,否则报异常
    getSettings.setBlockNetworkLoads(true);
  27. setJavaScriptEnabled(boolean flag)
    设置WebView是否允许执行JavaScript脚本,默认false,不允许
    getSettings.setJavaScriptEnabled(true);
  28. setAllowUniversalAccessFromFileURLs(boolean flag)
    设置WebView运行中的脚本可以是否访问任何原始起点内容,默认true
    getSettings.setAllowUniversalAccessFromFileURLs(false);
  29. setAllowFileAccessFromFileURLs(boolean flag)
    设置WebView运行中的一个文件方案被允许访问其他文件方案中的内容,默认值true
    getSettings.setAllowFileAccessFromFileURLs(false);
  30. setGeolocationDatabasePath(String databasePath)
    设置WebView保存地理位置信息数据路径,指定的路径Application具备写入权限
    getSettings.setGeolocationDatabasePath(String path);
  31. setAppCacheEnabled(boolean flag)
    设置Application缓存API是否开启,默认false,设置有效的缓存路径参考setAppCachePath(String path)方法
    getSettings.setAppCacheEnabled(true);
  32. setAppCachePath(String appCachePath)
    设置当前Application缓存文件路径,Application Cache API能够开启需要指定Application具备写入权限的路径
    getSettings.setAppCachePath(String appCachePath);
  33. setDatabaseEnabled(boolean flag)
    设置是否开启数据库存储API权限,默认false,未开启,可以参考setDatabasePath(String path)
    getSettings.setDatabaseEnabled(false);
  34. setDomStorageEnabled(boolean flag)
    设置是否开启DOM存储API权限,默认false,未开启,设置为true,WebView能够使用DOM storage API
    getSettings.setDomStorageEnabled(true);
  35. setGeolocationEnabled(boolean flag)
    设置是否开启定位功能,默认true,开启定位
    getSettings.setGeolocationEnabled(false);
  36. setJavaScriptCanOpenWindowsAutomatically(boolean flag)
    设置脚本是否允许自动打开弹窗,默认false,不允许
    getSettings.setJavaScriptCanOpenWindowsAutomatically(true);
  37. setDefaultTextEncodingName(String encoding)
    设置WebView加载页面文本内容的编码,默认“UTF-8”。
    getSettings.setDefaultTextEncodingName("UTF-8");
  38. setUserAgentString(String ua)
    设置WebView代理字符串,如果String为null或为空,将使用系统默认值
    getSettings.setUserAgentString(String ua);
  39. setNeedInitialFocus(boolean flag)
    设置WebView是否需要设置一个节点获取焦点当被回调的时候,默认true
    getSettings.setNeedInitialFocus(false);
  40. setCacheMode(int mode)
    重写缓存被使用到的方法,该方法基于Navigation Type,加载普通的页面,将会检查缓存同时重新验证是否需要加载,如果不需要重新加载,将直接从缓存读取数据,允许客户端通过指定LOAD_DEFAULT、LOAD_CACHE_ELSE_NETWORK、LOAD_NO_CACHE、LOAD_CACHE_ONLY其中之一重写该行为方法,默认值LOAD_DEFAULT
    getSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
  41. setMixedContentMode(int mode)
    设置当一个安全站点企图加载来自一个不安全站点资源时WebView的行为,android.os.Build.VERSION_CODES.KITKAT默认为MIXED_CONTENT_ALWAYS_ALLOW,android.os.Build.VERSION_CODES#LOLLIPOP默认为MIXED_CONTENT_NEVER_ALLOW,取值其中之一:MIXED_CONTENT_NEVER_ALLOW、MIXED_CONTENT_ALWAYS_ALLOW、MIXED_CONTENT_COMPATIBILITY_MODE.
    getSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);

你可能感兴趣的:(WebView控件之WebSettings各种配置方法源码总结)