WebSettings settings = mWebView.getSettings();
/**
* 设置userAgent用户表示,可以给服务器区分是否是移动端访问
*/
settings.setUserAgentString(settings.getUserAgentString().concat("userAgent"));
/**
* 根据menuItems标志禁用动作模式菜单项。
* WebSettings.MENU_ITEM_NONE :不应该禁用任何菜单项。值: 0
* WebSettings.MENU_ITEM_SHARE :禁用菜单项“共享”。值: 1
* WebSettings.MENU_ITEM_WEB_SEARCH :禁用菜单项“Web搜索”。值: 2
* WebSettings.MENU_ITEM_PROCESS_TEXT :禁用所有操作模式菜单项以进行文本处理。 默认情况下,WebView会搜索能够处理的活动。值: 4
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
settings.setDisabledActionModeMenuItems(WebSettings.MENU_ITEM_NONE);
}
/**
* 设置是否启用安全浏览。安全浏览允许WebView通过验证链接来防范恶意软件和网络钓鱼攻击。
* 可以使用清单标记为所有WebView禁用安全浏览,清单标记的优先级低于此API。
* 默认情况下,为支持安全浏览的设备启用安全浏览。
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
settings.setSafeBrowsingEnabled(true);
}
/**
* 存储(storage)
* 启用HTML5 DOM storage API,默认值 false
*/
settings.setDomStorageEnabled(true);
/**
* 启用Web SQL Database API,这个设置会影响同一进程内的所有WebView,默认值 false
* 此API已不推荐使用,参考:https://developer.android.google.cn/reference/kotlin/android/webkit/WebSettings
*/
settings.setDatabaseEnabled(true);
/**
* 启用Application Caches API,必需设置有效的缓存路径才能生效,默认值 false
* 此API已废弃,参考:https://developer.android.google.cn/reference/kotlin/android/webkit/WebSettings
*/
settings.setAppCacheEnabled(true);
/**
* 设置有效的缓存路径
* 与settings.setAppCacheEnabled(true);一起使用
*/
settings.setAppCachePath(context.getCacheDir().getAbsolutePath());
/**
* 定位(location)
*/
settings.setGeolocationEnabled(true);
/**
* 是否保存表单数据
*/
settings.setSaveFormData(true);
/**
* 是否当webview调用requestFocus时为页面的某个元素设置焦点,默认值 true
*/
settings.setNeedInitialFocus(true);
/**
* 是否支持viewport属性,默认值 false
* 页面通过`<meta name="viewport" ... />`自适应手机屏幕
*/
settings.setUseWideViewPort(true);
/**
* 是否使用overview mode加载页面,默认值 false
* 当页面宽度大于WebView宽度时,缩小使页面宽度等于WebView宽度
*/
settings.setLoadWithOverviewMode(true);
/**
* 布局算法
*/
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
/**
* 是否支持Javascript,默认值false
*/
settings.setJavaScriptEnabled(true);
/**
* 是否支持多窗口,默认值false
*/
settings.setSupportMultipleWindows(false);
/**
* 是否可用Javascript(window.open)打开窗口,默认值 false
*/
settings.setJavaScriptCanOpenWindowsAutomatically(false);
/**
* 资源访问
* 是否可访问Content Provider的资源,默认值 true
*/
settings.setAllowContentAccess(true);
/**
* 资源访问
* 是否可访问本地文件,默认值 true
*/
settings.setAllowFileAccess(true);
/**
* 是否允许通过file url加载的Javascript读取本地文件,默认值 false
*/
settings.setAllowFileAccessFromFileURLs(false);
/**
* 是否允许通过file url加载的Javascript读取全部资源(包括文件,http,https),默认值 false
*/
settings.setAllowUniversalAccessFromFileURLs(false);
/**
* 资源加载
* 是否自动加载图片
*/
settings.setLoadsImagesAutomatically(true);
/**
* 资源加载
* 禁止加载网络图片
*/
settings.setBlockNetworkImage(false);
/**
* 资源加载
* 禁止加载所有网络资源
*/
settings.setBlockNetworkLoads(false);
/**
* 缩放(zoom)
* 是否支持缩放
*/
settings.setSupportZoom(true);
/**
* 缩放(zoom)
* 是否使用内置缩放机制
*/
settings.setBuiltInZoomControls(false);
/**
* 缩放(zoom)
* 是否显示内置缩放控件
*/
settings.setDisplayZoomControls(true);
/**
* 默认文本编码,默认值 "UTF-8"
*/
settings.setDefaultTextEncodingName("UTF-8");
/**
* 默认文字尺寸,默认值16,取值范围1-72
*/
settings.setDefaultFontSize(16);
/**
* 默认等宽字体尺寸,默认值16
*/
settings.setDefaultFixedFontSize(16);
/**
* 最小文字尺寸,默认值 8
*/
settings.setMinimumFontSize(8);
/**
* 最小文字逻辑尺寸,默认值 8
*/
settings.setMinimumLogicalFontSize(8);
/**
* 文字缩放百分比,默认值 100
*/
settings.setTextZoom(100);
/**
* 字体
* 标准字体,默认值 "sans-serif"
*/
settings.setStandardFontFamily("sans-serif");
/**
* 衬线字体,默认值 "serif"
*/
settings.setSerifFontFamily("serif");
/**
* 无衬线字体,默认值 "sans-serif"
*/
settings.setSansSerifFontFamily("sans-serif");
/**
* 等宽字体,默认值 "monospace"
*/
settings.setFixedFontFamily("monospace");
/**
* 手写体(草书),默认值 "cursive"
*/
settings.setCursiveFontFamily("cursive");
/**
* 幻想体,默认值 "fantasy"
*/
settings.setFantasyFontFamily("fantasy");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
/**
* 用户是否需要通过手势播放媒体(不会自动播放),默认值 true
*/
settings.setMediaPlaybackRequiresUserGesture(true);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
/**
* 5.0以上允许加载http和https混合的页面(5.0以下默认允许,5.0+默认禁止)
*/
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
/**
* 是否在离开屏幕时光栅化(会增加内存消耗),默认值 false
*/
settings.setOffscreenPreRaster(false);
}
if (isNetworkConnected(context)) {
/**
* 设置缓存模式,根据cache-control决定是否从网络上取数据
* 缓存模式如下:
* LOAD_CACHE_ONLY: 不使用网络,只读取本地缓存数据。
* LOAD_DEFAULT: (默认)根据cache-control决定是否从网络上取数据。
* LOAD_NO_CACHE: 不使用缓存,只从网络获取数据。
* LOAD_CACHE_ELSE_NETWORK,只要本地有,无论是否过期,或者no-cache,都使用缓存中的数据。
*/
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
} else {
/**
* 没网,离线加载,优先加载缓存(即使已经过期)
*/
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
/**
* deprecated
* 过时,已经废弃的方法
*/
settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
settings.setDatabasePath(context.getDir("database", Context.MODE_PRIVATE).getPath());
settings.setGeolocationDatabasePath(context.getFilesDir().getPath());
webView系列(二)----WebSettings
最新推荐文章于 2025-03-26 21:03:58 发布