100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > android webview设置加载进度条

android webview设置加载进度条

时间:2021-01-13 12:28:22

相关推荐

android webview设置加载进度条

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; //进度条的高度,默认10pxTextView 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 };@Overrideprotected 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文本时,在弹出菜单前被调用。@Overridepublic ActionMode startActionMode(ActionMode.Callback callback) {ActionMode actionMode = startActionMode(callback);Log.e("hxw", actionMode.toString());return resolveActionMode(actionMode);}@Overridepublic 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() {@Overridepublic 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);//添加进度到WebViewaddView(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;}@Overridepublic 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() {@Overridepublic void onClick(DialogInterface dialog, int which) {result.confirm();}});b2.setCancelable(false);b2.create();b2.show();return true;}@Overridepublic 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="/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));

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。