1、自定义属性文件——attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
// X5Webview 是否支持默认进度条
<declare-styleable name="X5WebView">
<attr name="defaultProgress" format="boolean" />
</declare-styleable>
</resources>
2、自定义X5WebView
public class X5WebView extends WebView {
private ProgressBar progressbar; //进度条
private int progressHeight = 10; //进度条的高度,默认10px
TextView title;
private ActionMode mActionMode;
private long last_time = 0L;
private List<String> mActionList = new ArrayList<>();
private WebViewClient client = new WebViewClient() {
/**
* 防止加载网页时调起系统浏览器
*/
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
};
public OnScrollListener listener;
private static final int[] mAttr = { R.attr.defaultProgress };
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (listener != null){
if (t - oldt <= 2){
listener.onScrollDown();
}
if(oldt - t >= 2) {
listener.onScrollUp();
}
listener.scrollHeight(t);
}
}
public void setListener(OnScrollListener listener){
this.listener = listener;
}
public interface OnScrollListener{
void onScrollUp();//上滑
void onScrollDown();//下滑
void scrollHeight(int h);
}
//这两个方法会在用户长按选择web文本时,在弹出菜单前被调用。
@Override
public ActionMode startActionMode(ActionMode.Callback callback) {
ActionMode actionMode = startActionMode(callback);
Log.e("hxw", actionMode.toString());
return resolveActionMode(actionMode);
}
@Override
public ActionMode startActionMode(ActionMode.Callback callback, int type) {
ActionMode actionMode = startActionMode(callback, type);
Log.e("hxw", actionMode.toString() + " " + type);
return resolveActionMode(actionMode);
}
//处理item,处理点击
private ActionMode resolveActionMode(ActionMode actionMode) {
if (actionMode != null) {
final Menu menu = actionMode.getMenu();
mActionMode = actionMode;
menu.clear();
Log.e("hxw", mActionList.toString());
for (int i = 0; i < mActionList.size(); i++) {
menu.add(mActionList.get(i));
}
for (int i = 0; i < menu.size(); i++) {
MenuItem menuItem = menu.getItem(i);
menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// getSelectedData((String) item.getTitle());
// releaseAction();
return true;
}
});
}
}
mActionMode = actionMode;
return actionMode;
}
//设置弹出action列表
public void setActionList(List<String> actionList) {
mActionList = actionList;
}
@SuppressLint("SetJavaScriptEnabled")
public X5WebView(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
TypedArray ta = arg0.obtainStyledAttributes(arg1, mAttr);
Boolean enableDefaultProgress = ta.getBoolean(0, true);
if (!enableDefaultProgress) {
initWebViewSettings(arg0, false);
this.setWebChromeClient(new MyWebChromeClient(arg0));
} else {
initWebViewSettings(arg0, true);
this.setWebChromeClient(new MyWebChromeClient(arg0, progressbar));
}
this.setWebViewClient(client);
}
public void setProgressbarDrawable(Drawable d) {
if (progressbar != null) {
progressbar.setProgressDrawable(d);
}
}
private void initWebViewSettings(Context context, Boolean enable) {
if (enable) {
//创建进度条
progressbar = new ProgressBar(context, null,
android.R.attr.progressBarStyleHorizontal);
//设置加载进度条的高度
progressbar.setLayoutParams(new AbsoluteLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, progressHeight, 0, 0));
Drawable drawable = context.getResources().getDrawable(R.drawable.progressbar_business_area_red);
progressbar.setProgressDrawable(drawable);
//添加进度到WebView
addView(progressbar);
}
WebSettings webSetting = this.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
webSetting.setAllowFileAccess(true);
webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSetting.setSupportZoom(true);
webSetting.setBuiltInZoomControls(true);
webSetting.setUseWideViewPort(true);
webSetting.setSupportMultipleWindows(true);
// webSetting.setLoadWithOverviewMode(true);
webSetting.setAppCacheEnabled(false);
// webSetting.setDatabaseEnabled(true);
webSetting.setDomStorageEnabled(true);
webSetting.setGeolocationEnabled(true);
webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
webSetting.setTextSize(WebSettings.TextSize.NORMAL);
// webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
// webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
webSetting.setCacheMode(WebSettings.LOAD_DEFAULT);
webSetting.setBuiltInZoomControls(false);
webSetting.setDisplayZoomControls(false);
webSetting.setSupportZoom(false);
this.setWebContentsDebuggingEnabled(true);
String ua = webSetting.getUserAgentString();
// UserAgentParam up;
// if (UserNative.readIsLogin()) {
// up = new UserAgentParam(CommonString.appTag, UserNative.getId(), UserNative.getName(), UserNative.getPhone(), UserNative.getPwd(), UserNative.getEpId(), UserNative.getImage(), UserNative.getAesKes(), MyShareUtil.getSharedString("city"));
// } else {
// up = new UserAgentParam(CommonString.appTag, -1, "", "", "", "", "", "","");
// }
// webSetting.setUserAgent(ua + "&" + FastJsonUtils.toJSONString(up));
webSetting.setLoadsImagesAutomatically(true);
Log.e("hxw", webSetting.getUserAgentString());
// this.getSettingsExtension().setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);//extension
// settings 的设计
}
}
3、自定义弹窗处理类
public class MyWebChromeClient extends WebChromeClient {
private Context mContext;
private ProgressBar progressBar;
public MyWebChromeClient(Context context, ProgressBar progressBar) {
this.mContext = context;
this.progressBar = progressBar;
}
public MyWebChromeClient(Context context) {
this.mContext = context;
this.progressBar = null;
}
@Override
public boolean onJsAlert(WebView view, String url, String message,
final JsResult result) {
// 弹窗处理
AlertDialog.Builder b2 = new AlertDialog.Builder(mContext)
.setTitle(R.string.app_name).setMessage(message)
.setPositiveButton("确定", new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
});
b2.setCancelable(false);
b2.create();
b2.show();
return true;
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (progressBar != null) {
if (newProgress == 100) {
progressBar.setVisibility(View.GONE);
} else {
if (progressBar.getVisibility() == View.GONE)
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(newProgress);
}
} else {
// Logger.e("progress null");
}
super.onProgressChanged(view, newProgress);
}
}
4、自定义进度条drawable文件——progressbar_business_area_red.xml
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 设置背景色(白色) -->
<item android:id="@android:id/background">
<shape>
<!--<corners android:radius="3dip" />-->
<gradient android:startColor="@color/colorPrimaryDark"
android:centerColor="@color/colorPrimaryDark"
android:centerY="0.75"
android:endColor="@color/colorPrimaryDark"
android:angle="270"
/>
</shape>
</item>
<!-- 设置进度条颜色(蓝色) -->
<item android:id="@android:id/progress">
<clip>
<shape>
<!--<corners android:radius="3dip" /> -->
<gradient android:startColor="@color/colorAccent"
android:endColor="@color/colorAccent"/>
</shape>
</clip>
</item>
</layer-list>
5、引用
webView.setProgressbarDrawable(getResources().getDrawable(R.drawable.progressbar_business_area_red));