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="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));

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/415831.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

oracle 创建新库时报错:enterprise manager 配置失败

oracle新建库时遇到的问题总结 昨天新建库时按正常的操作&#xff0c;一路下来&#xff0c;当新建到85%时弹出错误如下图&#xff1a; 环境&#xff1a;2003 server 64bit  服务器&#xff08;&#xff29;&#xff22;&#xff2d;&#xff09; oracle 10G 64bit 原有二个…

iview form 表单 自定义参数验证 validate

需求&#xff0c;使用的是iview框架的Form组件&#xff0c;一般简单input类型数据&#xff0c;使用简单的验证配置就可以达到效果。如官方的写法。 iview 表单验证 地址 https://www.iviewui.com/components/form iview 默认验证库 async-validator 地址 https://github.com/y…

String类的一些常见的获取方法(5)

String s "aasfasfdtgsrast"; 1&#xff1a;  int a s.length() //返回字符串的长度 2&#xff1a;  char s1 charAt(int intdex); //返回指定索引位置的字符串 3&#xff1a;  int b intdexOf(String ch); //返回指定字符串在此字符串首次出现的位置 4&am…

小程序 地图 开发 组件 覆盖物

我的需求是 1、显示地图 2、在地图上增加覆盖物 3、地图距离底部边距有90rpx 主要使用到原生组件map和cover-view 实现效果&#xff1a; 代码我是使用的mpvue开发。源码如下&#xff1a; <template><div class"map-clock"><map id"map"…

android webview点击返回键返回上一页

重写两个返回方法即可&#xff1a; Override public void onBackPressed() {if (webView.canGoBack()) {webView.goBack();webView.removeAllViews();} else {super.onBackPressed();} }Override public boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode KeyEve…

如何找到哪些数据库中哪些用户有DBA权限

如何找到哪些用户有&#xff24;&#xff22;&#xff21;权限 我们有时候需要找到数据库服务器中&#xff0c;哪些用户具有DBA权限&#xff0c;以便于有关安全与权限管理。通常可以使用以下简单方法可以得到答案&#xff1a; 选择当前数据库&#xff0c; 执行SQL: 选择当前数…

mpvue 小程序 页面跳转获取参数

在mpvue中可以使用vuex来存储数据。但是在页面跳转传参方面&#xff0c;我是喜欢用其他写法。 小程序原生写法&#xff1a;https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateTo.html wx.navigateTo({url: test?id1 })Page({onLoad: function(option)…

关于逻辑删除标识字段value的设定

为了容易记忆&#xff0c;项目里所有表记录的逻辑删除可以用“-1”&#xff08;或其他值&#xff09;来表示。转载于:https://www.cnblogs.com/paulhe/p/7551614.html

system 无法以 sysdba 登录,提示:权限不足。

今天新建了一个数据库是oracle 10G64位,这个以前就建了二个实例&#xff0c;正在使用中&#xff0c;今天新建了一个实例&#xff0c;发现只有sys帐户可用sysdba登录&#xff0c;system无法以sysdba登录&#xff0c;但以nomal可以登录&#xff0c;记得以前建的新库&#xff0c;都…

android EditText光标位置,光标样式,EditText限制输入内容,软键盘遮挡的EditText,搜索框,限制输入表情

显示在软键盘上 android:windowSoftInputMode"adjustPan|stateHidden" 1、光标位置 homesosoedit.setSelection(homesosoedit.getText().length());// 2、指定输入内容 android:digits"0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"…

mpvue v-html 代替方法 使用 mpvue-wxparse

在vue项目中有时候会使用到v-html&#xff0c;而在mpvue中&#xff0c;也有代替品mpvue-wxparse。 1、安装mpvue-wxparse 2、使用&#xff0c;详情可见官网 https://github.com/F-loat/mpvue-wxParse <template><div><wxParse :content"article" p…

安装kali linux 2017.1 【二、安装VMware-tools 以及相关问题处理】

一、基本步骤&#xff1a; 1.VMware Workstation菜单栏中&#xff0c;选择“虚拟机”&#xff0c;”安装VMware Tools...“。 2.挂载VMware Tools安装程序到/mnt/cdrom/。 mkdir /mnt/cdrom mount /dev/cdrom /mnt/cdrom 看到...mounting read-only&#xff0c;表示挂载成功。 …

安装、卸载测试思路

安装卸载用例设计思路&#xff08;界面、易用方面的没写&#xff09; 一、安装路径&#xff1a; 1、缺省路径安装 2、自定义安装路径: a) 通过浏览&#xff0c;选择自定义路径 b) 手动输入路径&#xff08;存在的路径、不存在的路径&#xff09; c&#xff09;非C盘安装 d&…

vscode vetur 不想标签属性老是转行 配置

记得在工作区设置中配置如下 {"vetur.format.defaultFormatterOptions": {"js-beautify-html": {"wrap_line_length": 120,"wrap_attributes": "auto","end_with_newline": false}},"vetur.format.defaultF…

uoj#246. 【UER #7】套路(dp+分块?分类讨论?)

题目链接 分析&#xff1a; 目前为止我只能理解dp部分 我就喜欢这种单纯不做作的题目 一看名字就明白了这道题的本质 中二的题目描述 很显然&#xff0c;我们的关键就是求出最小相似度 朴素算法n^4 如果我们现在有一个权值数组 显然&#xff0c;每一个数只可能与最邻近ta的…

ARCSDE的直接连接(SQLSERVER)

ARCSDE的直接连接&#xff08;SQLSERVER&#xff09; 环境&#xff1a;windows 2003 (64bit) \oralce10G \sqlserver 2000现在想把另外一个数据库迁移过来&#xff0c;同时也需要迁移SDE&#xff0c;但现在服务器上已经安装了SDE for sql server &#xff0c;怎么办呢&…