概述
WebSettings
用于管理WebView
状态配置,当WebView
第一次被创建时,WebView
包含着一个默认的配置,这些默认的配置将通过get
方法返回,通过WebView
中的getSettings
方法获得一个WebSettings
对象
即如下调用:
1 | webview.getSettings().setJavaScriptEnabled(true); |
其次,下面这些方法没啥好说的,用到啥就过来查。
方法学习
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到72getSettings.setMinimumFontSize(8);
21. setMinimumLogicalFontSize(int size)
设置
WebView
逻辑上最小字体值,默认值8,取值1到72getSettings.setMinimumLogicalFontSize(8);
22. setDefaultFontSize(int size)
设置
WebView
默认值字体值,默认值16,取值1到72getSettings.setDefaultFontSize(16);
23. setDefaultFixedFontSize(int size)
设置
WebView
默认固定的字体值,默认值16,取值1到72getSettings.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)
34. setDomStorageEnabled(boolean flag)
设置是否开启
DOM
存储API
权限,默认false
,未开启,设置为true
,WebView
能够使用DOM storage API
getSettings.setDomStorageEnabled(true);
35. setGeolocationEnabled(boolean flag)
设置是否开启定位功能,默认
true
,开启定位getSettings.setGeolocationEnabled(false);
36. etJavaScriptCanOpenWindowsAutomatically(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);