Android中的webview的进度条
<application
android:icon="@drawable/hunqin"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light" >
主题 android:theme------->必须不能使Theme.Light.NoTitleBar
否则不起作用
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.progressbar);
setProgressBarVisibility(true);
<application
android:icon="@drawable/hunqin"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light.NoTitleBar" >
public class ProgressTest extends Activity{
final Activity context = this;
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
requestWindowFeature(Window.FEATURE_PROGRESS);//让进度条显示在标题栏上
setContentView(R.layout.main);
WebView webview = (WebView)findViewById(R.id.webview);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
//Activity和Webview根据加载程度决定进度条的进度大小
//当加载到100%的时候 进度条自动消失
context.setProgress(progress * 100);
}
});
webview.loadUrl(url);
}
http://www.cnblogs.com/over140/archive/2013/03/07/2947721.html
前言
如果不使用系统自带的TitleBar(即Activity被设置@android:style/Theme.NoTitleBar),那就需要自己来写进度条了,这里封装了一个自定义控件和加载网页的公共Activity,方便使用。
声明
欢迎转载,但请保留文章原始出处:)
博客园:http://www.cnblogs.com
农民伯伯: http://over140.cnblogs.com
正文
一、截图

二、自定义控件

* 带进度条的WebView
* @author 农民伯伯
* @see http://www.cnblogs.com/over140/archive/2013/03/07/2947721.html
*
*/
@SuppressWarnings("deprecation")
public class ProgressWebView extends WebView {
private ProgressBar progressbar;
public ProgressWebView(Context context, AttributeSet attrs) {
super(context, attrs);
progressbar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
progressbar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 3, 0, 0));
addView(progressbar);
// setWebViewClient(new WebViewClient(){});
setWebChromeClient(new WebChromeClient());
}
public class WebChromeClient extends android.webkit.WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
progressbar.setVisibility(GONE);
} else {
if (progressbar.getVisibility() == GONE)
progressbar.setVisibility(VISIBLE);
progressbar.setProgress(newProgress);
}
super.onProgressChanged(view, newProgress);
}
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
LayoutParams lp = (LayoutParams) progressbar.getLayoutParams();
lp.x = l;
lp.y = t;
progressbar.setLayoutParams(lp);
super.onScrollChanged(l, t, oldl, oldt);
}
}

三、加载网页的公共Activity

* 加载网页的Activity
*
* @author 农民伯伯
* @see http://www.cnblogs.com/over140/archive/2013/03/07/2947721.html
*
*/
public class WebActivity extends BaseActivity {
private ProgressWebView webview;
private String url;
private String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.commom_web);
// ~~~ 获取参数
url = getIntent().getStringExtra("url");
name = getIntent().getStringExtra("name");
// ~~~ 绑定控件
webview = (ProgressWebView) findViewById(R.id.webview);
// ~~~ 设置数据
titleText.setText(name);
webview.getSettings().setJavaScriptEnabled(true);
webview.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
if (url != null && url.startsWith("http://"))
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
});
webview.loadUrl(url);
}
}

commom_web.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include layout="@layout/include_title" />
<com.nmbb.ui.widget.ProgressWebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>

四、补充说明
1、还可以再优化一下,在标题栏加一个刷新按钮。
2、如果加载的页面有需要下载文件,需要设置setDownloadListener方法,根据项目实际需求定制。
3、自定义控件是在转载的,忘记出处,感谢~~
结束
没啥高深技术,实用就行!
Android中的webview的进度条的更多相关文章
- Android笔记(二十三) Android中的ProgressBar(进度条)
圆形进度条和水平进度条 进度条也是UI界面一种非常实用的组件,通常用于向用户显示某个耗时操作完成的百分比,进度条可以动态的显示进度,避免长时间的执行某个耗时操作时,让用户感觉程序失去了相应,从而更好的 ...
- Android中使用WebView, WebChromeClient和WebViewClient加载网页 (能够执行js)
Android中使用WebView, WebChromeClient和WebViewClient加载网页 在android应用中,有时要加载一个网页,如果能配上一个进度条就更好了,而android ...
- WebView线性进度条
参考:http://www.cnblogs.com/hubli/p/4835549.html APP会跳转网页时候,请参考:http://blog.csdn.net/raphael55/article ...
- webview的进度条的加载,webview的使用以及handle的理解与使用
Webview的几个关键方法要介绍一些: 谷歌官方文档是这么说的; A WebView has several customization points where you can add your ...
- android 开发-自定义多节点进度条显示
看效果图: 里面的线段颜色和节点图标都是可以自定义的. main.xml <RelativeLayout xmlns:android="http://schemas.android.c ...
- Android ProgressBar实现加载进度条
progressBar Android进度条组件. progressBar的关键属性: android:max="100" 最大显示进度条 andr ...
- Android中脱离WebView使用WebSocket实现群聊和推送功能
WebSocket是Web2.0时代的新产物,用于弥补HTTP协议的某些不足,不过他们之间真实的关系是兄弟关系,都是对socket的进一步封装,其目前最直观的表现就是服务器推送和聊天功能.更多知识参考 ...
- Android 中的 WebView实现 Html5 标签网页加载
自Android 4.4起,Android中的WebView开始基于Chromium(谷歌浏览器)支持浏览器的一系列功能,webkit解析网页各个节点,这个改变,使得WebView的性能大幅度提升,并 ...
- Android中通过WebView控件实现与JavaScript方法相互调用的地图应用
在Android中通过WebView控件,可以实现要加载的页面与Android方法相互调用,我们要实现WebView中的addJavascriptInterface方法,这样html才能调用andro ...
随机推荐
- 大暴力——[HAOI]2012音量调节
题目:[HAOI]2012音量调节 描述: 问题描述 一个吉他手准备参加一场演出.他不喜欢在演出时始终使用同一个音量,所以他决定每一首歌之前他都要改变一次音量.在演出开始之前,他已经做好了一个列表,里 ...
- hdu-1242 dfs+各种剪枝
思路: 可以和1010一个思路.这个抽象的说就是让你求给定图中两点的最短距离,其实dfs的题目能变化的地方就在“终点的回溯处”,在我们到达终点后,再判断一些附加的值(本题里是最短距离是否更新),从而得 ...
- SpringMVC框架图解析
Spring框架提供了构造Web应用程序的全能MVC模块.Spring MVC分离了控制器.模型对象.分派器以及处理程序对象的角色,这种分离让它们更容易进行制定.是一个标准的MVC框架. 那你猜一猜哪 ...
- python常用的一些东西——sys、os等
1.常用内置函数:(不用import就可以直接使用) help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像函数一样调用 ...
- C primer plus 读书笔记第六章和第七章
这两章的标题是C控制语句:循环以及C控制语句:分支和跳转.之所以一起讲,是因为这两章内容都是讲控制语句. 第六章的第一段示例代码 /* summing.c --对用户输入的整数求和 */ #inclu ...
- 安装LVS安装LVS和配置LVS的工作比较繁杂
安装LVS安装LVS和配置LVS的工作比较繁杂,读者在配置的过程中需要非常细心和耐心.在本节我们将对其进行详细地介绍.主要包括如下几个核心步骤:1.获取支持LVS的内核源代码如果读者需要使用LVS,需 ...
- tomcat安全配置之禁用Directory Listing
什么是Directory Listing?通俗点讲,就是在webapp的目录下如果没有放置index.html或者类似的文件,如果从IE或者其它浏览器文章这个路径时,会惊喜的发现这个目录下的文件列表被 ...
- ListIterator add remove 使用注意
add方法示例 //在最前面添加 List<String> list1 = new LinkedList<String>(Arrays.asList(new String[] ...
- poj 2823 Sliding Window(单调队列)
/* 裸地单调队列.. 第一次写 写的好丑.... */ #include<iostream> #include<cstdio> #include<cstring> ...
- DIV布局之道二:DIV块的嵌套,DIV盒子模型
本文讲解DIV块布局的第二种使用方式:嵌套.“DIV嵌套”在有些文献中也被称为“盒子模型”,说的通俗一点就是嵌套(一个大的DIV块内部又包含一个或多个DIV块). 请看如下代码: CSS部分: CSS ...