下面列出了android.webkit.WebSettings#setBlockNetworkImage ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initWebView() {
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
/*使webview能够加载图片*/
webSettings.setBlockNetworkImage(false);
webSettings.setAppCacheEnabled(true);
mDialog=new SpotsDialog(this);
mDialog.show();
mWebView.loadUrl(mMessage.getClickUrl());
// mWebAppInterface = new WebAppInterface(this);
// mWebView.addJavascriptInterface(mWebAppInterface, "appInterface");
mWebView.setWebViewClient(new MyWebChromeC());
mWebView.setWebChromeClient(new WebChromeClient(){
});
}
private void init() {
if (isInEditMode()) {
return;
}
WebSettings settings = getSettings();
settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(false);
//设置缓存模式
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//开启DOM storage API功能
settings.setDomStorageEnabled(true);
//开启database storage 功能
settings.setDatabaseEnabled(true);
String cacheDir = getContext().getFilesDir().getAbsolutePath() + "web_cache";
settings.setAppCachePath(cacheDir);
settings.setAppCacheEnabled(true);
settings.setLoadsImagesAutomatically(true);
settings.setDefaultTextEncodingName(ENCODING_UTF_8);
settings.setBlockNetworkImage(false);
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
setHorizontalScrollBarEnabled(false);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setSupportActionBar(R.id.browser_toolbar);
final String url = getIntent().getStringExtra(EXTRA_URL);
if (TextUtils.isEmpty(url)) {
finish();
return;
}
setTitle("");
mVContent = findViewById(R.id.browser_wb_content);
WebSettings webSettings = mVContent.getSettings();
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setSupportZoom(true);
webSettings.setNeedInitialFocus(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setBlockNetworkImage(false);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setDisplayZoomControls(false);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
if (Build.VERSION.SDK_INT >= 21) {
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
webSettings.setDefaultTextEncodingName("utf-8");
mVContent.setWebViewClient(new PowerfulWebView.StateWebViewClient());
mVContent.setOnTitleListener(this);
mVContent.loadUrl(url);
}
private void setupWebViewSettings() {
final WebSettings webSettings = subredditDesc.getSettings();
Resources res = getResources();
float fontSize = res.getDimension(R.dimen.web_text);
webSettings.setDefaultFontSize((int) fontSize);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webSettings.setAppCacheEnabled(false);
webSettings.setBlockNetworkImage(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setGeolocationEnabled(false);
webSettings.setNeedInitialFocus(false);
webSettings.setSaveFormData(false);
}
@SuppressLint("SetJavaScriptEnabled") private void initView(@Nullable AttributeSet attrs) {
if (isInEditMode()) return;
if (attrs != null) {
TypedArray tp = getContext().obtainStyledAttributes(attrs, R.styleable.PrettifyWebView);
try {
int color = tp.getColor(R.styleable.PrettifyWebView_webview_background, ViewHelper.getWindowBackground(getContext()));
setBackgroundColor(color);
} finally {
tp.recycle();
}
}
setWebChromeClient(new ChromeClient());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setWebViewClient(new WebClient());
} else {
setWebViewClient(new WebClientCompat());
}
WebSettings settings = getSettings();
settings.setJavaScriptEnabled(true);
settings.setAppCachePath(getContext().getCacheDir().getPath());
settings.setAppCacheEnabled(true);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setDefaultTextEncodingName("utf-8");
settings.setLoadsImagesAutomatically(true);
settings.setBlockNetworkImage(false);
setOnLongClickListener((view) -> {
WebView.HitTestResult result = getHitTestResult();
if (hitLinkResult(result) && !InputHelper.isEmpty(result.getExtra())) {
AppHelper.copyToClipboard(getContext(), result.getExtra());
return true;
}
return false;
});
}
@SuppressLint("SetJavaScriptEnabled")
private void handleSetting() {
WebSettings ws = mWebView.getSettings();
// 保存表单数据
ws.setSaveFormData(true);
// 是否应该支持使用其屏幕缩放控件和手势缩放
ws.setSupportZoom(true);
ws.setBuiltInZoomControls(true);
ws.setDisplayZoomControls(false);
// 启动应用缓存
ws.setAppCacheEnabled(true);
// 设置缓存模式
ws.setCacheMode(WebSettings.LOAD_DEFAULT);
// setDefaultZoom api19被弃用
// 网页内容的宽度自适应屏幕
ws.setLoadWithOverviewMode(true);
ws.setUseWideViewPort(true);
// 告诉WebView启用JavaScript执行。默认的是false。
ws.setJavaScriptEnabled(true);
// 页面加载好以后,再放开图片
ws.setBlockNetworkImage(false);
// 使用localStorage则必须打开
ws.setDomStorageEnabled(true);
// 排版适应屏幕
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
} else {
ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
}
// WebView是否新窗口打开(加了后可能打不开网页)
// ws.setSupportMultipleWindows(true);
// webview从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
/** 设置字体默认缩放大小(改变网页字体大小,setTextSize api14被弃用)*/
ws.setTextZoom(100);
}
@Override
public void applyAppSettings(@NotNull Context context, @NotNull WebSettings webSettings, @NotNull SystemWebView systemWebView) {
// Clear the cache so trackers previously loaded are removed
systemWebView.clearCache(true);
// We could consider calling setLoadsImagesAutomatically() here too (This will block images not loaded over the network too)
webSettings.setBlockNetworkImage(Settings.getInstance(context).shouldBlockImages());
webSettings.setJavaScriptEnabled(!Settings.getInstance(context).shouldBlockJavaScript());
CookieManager.getInstance().setAcceptThirdPartyCookies(systemWebView, !Settings.getInstance
(context).shouldBlockThirdPartyCookies());
CookieManager.getInstance().setAcceptCookie(!Settings.getInstance(context)
.shouldBlockCookies());
}
@Override
@SuppressLint("SetJavaScriptEnabled") // We explicitly want to enable JavaScript
public void disableBlocking(@NotNull WebSettings webSettings, @NotNull SystemWebView systemWebView) {
webSettings.setBlockNetworkImage(false);
webSettings.setJavaScriptEnabled(true);
CookieManager.getInstance().setAcceptThirdPartyCookies(systemWebView, true);
CookieManager.getInstance().setAcceptCookie(true);
}
private void setWebView() {
WebSettings settings = mWebView.getSettings();
settings.setSupportZoom(true); //支持缩放
settings.setBlockNetworkImage(true); //设置图片最后加载
// settings.setBlockNetworkLoads ( true );
// settings.setDomStorageEnabled ( true );
settings.setDatabaseEnabled(true);
// String cacheDirPath = mContext.getFilesDir ( ).getAbsolutePath()+ CacheUtil.WEB_CACAH_DIRNAME;
//缓存
// settings.setAppCachePath ( );
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
settings.setAppCacheEnabled(true);
// settings.setAppCachePath ( cacheDirPath );
// settings.setBuiltInZoomControls ( true ); //启用内置缩放装置
settings.setJavaScriptEnabled(true); //启用JS脚本
/* mWebView.setOnKeyListener ( new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
mWebView.goBack(); //后退
return true;
}
}
return false;
}
});*/
}
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void initData() {
mAgentWeb = AgentWeb.with(this)
.setAgentWebParent(mContainerFrameLayout, new LinearLayout.LayoutParams(-1, -1))
.useDefaultIndicator(getResources().getColor(R.color.colorPrimary))
.setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)
.createAgentWeb()
.ready()
.go(mUrl);
WebView mWebView = mAgentWeb.getWebCreator().getWebView();
WebSettings mSettings = mWebView.getSettings();
mSettings.setAppCacheEnabled(true);
mSettings.setDomStorageEnabled(true);
mSettings.setDatabaseEnabled(true);
//判断是否为无图模式
if(mPresenter.getNoImgStyleState()){
mSettings.setBlockNetworkImage(true);
}else{
mSettings.setBlockNetworkImage(false);
}
//判断是否为自动缓存模式
if (mPresenter.getAutoCacheState()) {
mSettings.setAppCacheEnabled(true);
mSettings.setDomStorageEnabled(true);
mSettings.setDatabaseEnabled(true);
if (CommonUtils.isNetworkConnected(App.getContext())) {
mSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
} else {
mSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
} else {
mSettings.setAppCacheEnabled(false);
mSettings.setDomStorageEnabled(false);
mSettings.setDatabaseEnabled(false);
mSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
}
mSettings.setJavaScriptEnabled(true);
mSettings.setSupportZoom(true);
mSettings.setBuiltInZoomControls(true);
//不显示缩放按钮
mSettings.setDisplayZoomControls(false);
//设置自适应屏幕,两者合用
//将图片调整到适合WebView的大小
mSettings.setUseWideViewPort(true);
//缩放至屏幕的大小
mSettings.setLoadWithOverviewMode(true);
//自适应屏幕
mSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
}
@SuppressLint("JavascriptInterface")
private void initWebView() {
WebSettings ws = mWebView.getSettings();
// 网页内容的宽度是否可大于WebView控件的宽度
ws.setLoadWithOverviewMode(false);
// 保存表单数据
ws.setSaveFormData(true);
// 是否应该支持使用其屏幕缩放控件和手势缩放
ws.setSupportZoom(true);
ws.setBuiltInZoomControls(true);
ws.setDisplayZoomControls(false);
// 启动应用缓存
ws.setAppCacheEnabled(true);
// 设置缓存模式
ws.setCacheMode(WebSettings.LOAD_DEFAULT);
// setDefaultZoom api19被弃用
// 设置此属性,可任意比例缩放。
ws.setUseWideViewPort(true);
// 缩放比例 1
mWebView.setInitialScale(1);
// 告诉WebView启用JavaScript执行。默认的是false。
ws.setJavaScriptEnabled(true);
//如果启用了JavaScript,要做好安全措施,防止远程执行漏洞
removeJavascriptInterfaces(mWebView);
// 页面加载好以后,再放开图片
ws.setBlockNetworkImage(false);
// 使用localStorage则必须打开
ws.setDomStorageEnabled(true);
//自动加载图片
ws.setLoadsImagesAutomatically(true);
// 排版适应屏幕
ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
// WebView是否支持多个窗口。
ws.setSupportMultipleWindows(true);
// webView从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
/*设置字体默认缩放大小(改变网页字体大小,setTextSize api14被弃用)*/
ws.setTextZoom(100);
//在js中调用本地java方法
jsAppInterface = new JsAppInterface(this, mWebView);
jsAppInterface.register();
mWebView.addJavascriptInterface(jsAppInterface, "WebViewJsMethodName");
mWebView.addJavascriptInterface(new JavascriptInterface(this), "injectedObject");
mWebView.setWebViewClient(new MyWebViewClient());
mWebView.setWebChromeClient(webChromeClient = new MyWebChromeClient());
mWebView.setScrollWebListener(new ScrollWebView.OnScrollWebListener() {
@Override
public void onScroll(int dx, int dy) {
//WebView的总高度
float webViewContentHeight = mWebView.getContentHeight() * mWebView.getScale();
//WebView的现高度
float webViewCurrentHeight = (mWebView.getHeight() + mWebView.getScrollY());
LogUtils.e("webViewContentHeight=" + webViewContentHeight);
LogUtils.e("webViewCurrentHeight=" + webViewCurrentHeight);
if ((webViewContentHeight - webViewCurrentHeight) == 0) {
LogUtils.e("WebView滑动到了底端");
}
}
});
}
public synchronized void initPreferences() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
WebSettings webSettings = getSettings();
webSettings.setLoadWithOverviewMode(true);
webSettings.setTextZoom(100);
webSettings.setUseWideViewPort(true);
webSettings.setBlockNetworkImage(!sp.getBoolean(context.getString(R.string.sp_images), true));
webSettings.setJavaScriptEnabled(sp.getBoolean(context.getString(R.string.sp_javascript), true));
webSettings.setJavaScriptCanOpenWindowsAutomatically(sp.getBoolean(context.getString(R.string.sp_javascript), true));
webSettings.setGeolocationEnabled(sp.getBoolean(context.getString(R.string.sp_location), true));
webSettings.setSupportMultipleWindows(sp.getBoolean(context.getString(R.string.sp_multiple_windows), false));
webSettings.setSaveFormData(sp.getBoolean(context.getString(R.string.sp_passwords), true));
boolean textReflow = sp.getBoolean(context.getString(R.string.sp_text_reflow), true);
if (textReflow) {
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
try {
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
} catch (Exception e) {}
}
} else {
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
}
int userAgent = Integer.valueOf(sp.getString(context.getString(R.string.sp_user_agent), "0"));
if (userAgent == 1) {
webSettings.setUserAgentString(BrowserUnit.UA_DESKTOP);
} else if (userAgent == 2) {
webSettings.setUserAgentString(sp.getString(context.getString(R.string.sp_user_agent_custom), userAgentOriginal));
} else {
webSettings.setUserAgentString(userAgentOriginal);
}
int mode = Integer.valueOf(sp.getString(context.getString(R.string.sp_rendering), "0"));
initRendering(mode);
webViewClient.enableAdBlock(sp.getBoolean(context.getString(R.string.sp_ad_block), true));
}
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void initEventAndData() {
mAgentWeb = AgentWeb.with(this)
.setAgentWebParent(mWebContent, new LinearLayout.LayoutParams(-1, -1))
.useDefaultIndicator()
.setMainFrameErrorView(R.layout.webview_error_view, -1)
.createAgentWeb()
.ready()
.go(articleLink);
WebView mWebView = mAgentWeb.getWebCreator().getWebView();
WebSettings mSettings = mWebView.getSettings();
if (mPresenter.getNoImageState()) {
mSettings.setBlockNetworkImage(true);
} else {
mSettings.setBlockNetworkImage(false);
}
if (mPresenter.getAutoCacheState()) {
mSettings.setAppCacheEnabled(true);
mSettings.setDomStorageEnabled(true);
mSettings.setDatabaseEnabled(true);
if (CommonUtils.isNetworkConnected()) {
mSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
} else {
mSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
} else {
mSettings.setAppCacheEnabled(false);
mSettings.setDomStorageEnabled(false);
mSettings.setDatabaseEnabled(false);
mSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
}
mSettings.setJavaScriptEnabled(true);
mSettings.setSupportZoom(true);
mSettings.setBuiltInZoomControls(true);
//不显示缩放按钮
mSettings.setDisplayZoomControls(false);
//设置自适应屏幕,两者合用
//将图片调整到适合WebView的大小
mSettings.setUseWideViewPort(true);
//缩放至屏幕的大小
mSettings.setLoadWithOverviewMode(true);
//自适应屏幕
mSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
}
private void initWebView() {
mProgressBar.setVisibility(View.VISIBLE);
WebSettings ws = webView.getSettings();
// 网页内容的宽度是否可大于WebView控件的宽度
ws.setLoadWithOverviewMode(false);
// 保存表单数据
ws.setSaveFormData(true);
// 是否应该支持使用其屏幕缩放控件和手势缩放
ws.setSupportZoom(true);
ws.setBuiltInZoomControls(true);
ws.setDisplayZoomControls(false);
// 启动应用缓存
ws.setAppCacheEnabled(true);
// 设置缓存模式
ws.setCacheMode(WebSettings.LOAD_DEFAULT);
// setDefaultZoom api19被弃用
// 设置此属性,可任意比例缩放。
ws.setUseWideViewPort(true);
// 不缩放
webView.setInitialScale(100);
// 告诉WebView启用JavaScript执行。默认的是false。
ws.setJavaScriptEnabled(true);
// 页面加载好以后,再放开图片
ws.setBlockNetworkImage(false);
// 使用localStorage则必须打开
ws.setDomStorageEnabled(true);
// 排版适应屏幕
ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
// WebView是否新窗口打开(加了后可能打不开网页)
// ws.setSupportMultipleWindows(true);
// webview从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
/** 设置字体默认缩放大小(改变网页字体大小,setTextSize api14被弃用)*/
ws.setTextZoom(100);
mWebChromeClient = new MyWebChromeClient(this);
webView.setWebChromeClient(mWebChromeClient);
// 与js交互
webView.addJavascriptInterface(new MyJavascriptInterface(this), "injectedObject");
webView.setWebViewClient(new MyWebViewClient(this));
webView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return handleLongImage();
}
});
}
@SuppressLint("SetJavaScriptEnabled")
private void initWebView() {
WebSettings ws = webView.getSettings();
// 网页内容的宽度适配
ws.setLoadWithOverviewMode(true);
ws.setUseWideViewPort(true);
// 保存表单数据
ws.setSaveFormData(true);
// 是否应该支持使用其屏幕缩放控件和手势缩放
ws.setSupportZoom(true);
ws.setBuiltInZoomControls(true);
ws.setDisplayZoomControls(false);
// 启动应用缓存
ws.setAppCacheEnabled(true);
// 设置缓存模式
ws.setCacheMode(WebSettings.LOAD_DEFAULT);
// 告诉WebView启用JavaScript执行。默认的是false。
ws.setJavaScriptEnabled(true);
// 页面加载好以后,再放开图片
ws.setBlockNetworkImage(false);
// 使用localStorage则必须打开
ws.setDomStorageEnabled(true);
// 排版适应屏幕
ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
// WebView是否新窗口打开(加了后可能打不开网页)
// ws.setSupportMultipleWindows(true);
// webview从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
/** 设置字体默认缩放大小(改变网页字体大小,setTextSize api14被弃用)*/
ws.setTextZoom(100);
mWebChromeClient = new MyWebChromeClient(this);
webView.setWebChromeClient(mWebChromeClient);
// 与js交互
webView.addJavascriptInterface(new ImageClickInterface(this), "injectedObject");
webView.setWebViewClient(new MyWebViewClient(this));
webView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return handleLongImage();
}
});
}
public WebView(Context context) {
super(context);
WebSettings settings = getSettings();
if(Build.VERSION.SDK_INT >= 16) {
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
}
if (Build.VERSION.SDK_INT >= 19) {
try {
_evaluateJavascript = android.webkit.WebView.class.getMethod("evaluateJavascript", String.class, ValueCallback.class);
} catch(NoSuchMethodException e) {
DeviceLog.exception("Method evaluateJavascript not found", e);
_evaluateJavascript = null;
}
}
settings.setAppCacheEnabled(false);
settings.setBlockNetworkImage(false);
settings.setBlockNetworkLoads(false);
settings.setBuiltInZoomControls(false);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setDatabaseEnabled(false);
if(Build.VERSION.SDK_INT >= 11) {
settings.setDisplayZoomControls(false);
}
settings.setDomStorageEnabled(false);
if(Build.VERSION.SDK_INT >= 11) {
settings.setEnableSmoothTransition(false);
}
settings.setGeolocationEnabled(false);
settings.setJavaScriptCanOpenWindowsAutomatically(false);
settings.setJavaScriptEnabled(true);
settings.setLightTouchEnabled(false);
settings.setLoadWithOverviewMode(false);
settings.setLoadsImagesAutomatically(true);
if(Build.VERSION.SDK_INT >= 17) {
settings.setMediaPlaybackRequiresUserGesture(false);
}
if(Build.VERSION.SDK_INT >= 21) {
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW);
}
settings.setNeedInitialFocus(true);
settings.setPluginState(WebSettings.PluginState.OFF);
settings.setRenderPriority(WebSettings.RenderPriority.NORMAL);
settings.setSaveFormData(false);
settings.setSavePassword(false);
settings.setSupportMultipleWindows(false);
settings.setSupportZoom(false);
settings.setUseWideViewPort(true);
setHorizontalScrollBarEnabled(false);
setVerticalScrollBarEnabled(false);
setInitialScale(0);
setBackgroundColor(Color.TRANSPARENT);
ViewUtilities.setBackground(this, new ColorDrawable(Color.TRANSPARENT));
setBackgroundResource(0);
addJavascriptInterface(new WebViewBridgeInterface(), "webviewbridge");
}
/**
* 初始化WebSetting
*
* @param settings WebSetting
*/
protected void initWebSetting(WebSettings settings) {
// 缩放至屏幕的大小
settings.setLoadWithOverviewMode(true);
// 保存表单数据
settings.setSaveFormData(true);
// 是否应该支持使用其屏幕缩放控件和手势缩放
settings.setSupportZoom(true);
// //是否支持手势缩放控制
// settings.setBuiltInZoomControls(true);
// 是否隐藏原生的缩放控件
// settings.setDisplayZoomControls(false);
// 启动应用缓存
settings.setAppCacheEnabled(true);
// 排版适应屏幕,只显示一列
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
// settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
// 页面加载好以后,再放开图片
settings.setBlockNetworkImage(false);
// 使用localStorage则必须打开
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
// WebView启用JavaScript执行。默认的是false。
settings.setJavaScriptEnabled(true); // 设置支持javascript脚本
settings.setJavaScriptCanOpenWindowsAutomatically(true);
if (NetworkConnectionUtils.isConnected(mContext)) {
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
} else {
settings.setCacheMode(WebSettings.LOAD_CACHE_ONLY);
}
// settings.setBlockNetworkImage(false);
// settings.setAppCacheEnabled(true);
// settings.setDomStorageEnabled(true);
// settings.setDatabaseEnabled(true);
// if (NetworkConnectionUtils.isConnected(mContext)) {
// settings.setCacheMode(WebSettings.LOAD_DEFAULT);
// } else {
// settings.setCacheMode(WebSettings.LOAD_CACHE_ONLY);
// }
// settings.setJavaScriptEnabled(true);
// settings.setLoadWithOverviewMode(true);
// settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
// settings.setSupportZoom(true);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void setUpWebViewDefaults(WebView webView) {
WebSettings settings = webView.getSettings();
// 网页内容的宽度是否可大于WebView控件的宽度
settings.setLoadWithOverviewMode(false);
// 保存表单数据
settings.setSaveFormData(true);
// 是否应该支持使用其屏幕缩放控件和手势缩放
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
settings.setDisplayZoomControls(false);
// 启动应用缓存
settings.setAppCacheEnabled(true);
// 设置缓存模式
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
// setDefaultZoom api19被弃用
// 设置此属性,可任意比例缩放。
settings.setUseWideViewPort(true);
// 缩放比例 1
webView.setInitialScale(1);
// 告诉WebView启用JavaScript执行。默认的是false。
settings.setJavaScriptEnabled(true);
// 页面加载好以后,再放开图片
settings.setBlockNetworkImage(false);
// 使用localStorage则必须打开
settings.setDomStorageEnabled(true);
// 排版适应屏幕
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
// WebView是否支持多个窗口。
settings.setSupportMultipleWindows(true);
// webview从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
/** 设置字体默认缩放大小(改变网页字体大小,setTextSize api14被弃用)*/
settings.setTextZoom(100);
// Enable remote debugging via chrome://inspect
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}
// AppRTC requires third party cookies to work
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptThirdPartyCookies(webView, true);
}
@SuppressLint({"SetJavaScriptEnabled"})
private void setWebViewAttributes(WebView wv) {
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBlockNetworkImage(true);
}
@SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"})
private void initWebView() {
WebSettings ws = webView.getSettings();
// 保存表单数据
ws.setSaveFormData(true);
// 是否应该支持使用其屏幕缩放控件和手势缩放
ws.setSupportZoom(true);
ws.setBuiltInZoomControls(true);
ws.setDisplayZoomControls(false);
// 启动应用缓存
ws.setAppCacheEnabled(true);
// 设置缓存模式
ws.setCacheMode(WebSettings.LOAD_DEFAULT);
// setDefaultZoom api19被弃用
// 网页内容的宽度自适应屏幕
ws.setLoadWithOverviewMode(true);
ws.setUseWideViewPort(true);
// 网页缩放至100,一般的网页达到屏幕宽度效果,个别除外
// webView.setInitialScale(100);
// 关掉下滑弧形阴影
// webView.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
// 告诉WebView启用JavaScript执行。默认的是false。
ws.setJavaScriptEnabled(true);
// 页面加载好以后,再放开图片
ws.setBlockNetworkImage(false);
// 使用localStorage则必须打开
ws.setDomStorageEnabled(true);
// 排版适应屏幕
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
} else {
ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
}
// WebView是否新窗口打开(加了后可能打不开网页)
// ws.setSupportMultipleWindows(true);
// webview从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
/** 设置字体默认缩放大小(改变网页字体大小,setTextSize api14被弃用)*/
ws.setTextZoom(100);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
webView.setScrollBarSize(WebTools.dp2px(this, 3));
}
mWebChromeClient = new MyWebChromeClient(this);
webView.setWebChromeClient(mWebChromeClient);
// 与js交互
webView.addJavascriptInterface(new MyJavascriptInterface(this), "injectedObject");
webView.setWebViewClient(new MyWebViewClient(this));
webView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return handleLongImage();
}
});
}