服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android WebView线性进度条实例详解

Android WebView线性进度条实例详解

2021-05-14 15:43路边桥凉 Android

这篇文章主要介绍了Android WebView线性进度条实例详解的相关资料,需要的朋友可以参考下

推荐阅读:android webview添加网页加载进度条实例详解

先给大家展示下效果图:这个效果图大家一看就懂,在生活经常见到

Android WebView线性进度条实例详解

1.wevbview_progressbar.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 背景 -->
<item android:id="@android:id/background">
<shape>
<solid android:color="@android:color/transparent" />
</shape>
</item>
<!-- 进度条 -->
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="@android:color/holo_green_light" />
</shape>
</clip>
</item>
</layer-list>

2.progresswebview.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
@suppresswarnings("deprecation")public class progresswebview extends webview
{
private final static string tag = progresswebview.class.getsimplename();
private progressbar progressbar;
private context context;
public progresswebview(context context, attributeset attrs)
{
super(context, attrs);
this.context = context;
progressbar = new progressbar(context, null, android.r.attr.progressbarstylehorizontal);
progressbar.setlayoutparams(new absolutelayout.layoutparams(absolutelayout.layoutparams.match_parent, 3, 0, 0)); progressbar.setprogressdrawable(getresources().getdrawable(r.drawable.wevbview_progressbar));
addview(progressbar);
setwebchromeclient(new webchromeclient());
}
public class webchromeclient extends android.webkit.webchromeclient
{
@override
public void onprogresschanged(webview view, int newprogress)
{
log.d(tag, "newprogress" + newprogress);
if (newprogress == 100)
{
progressbar.setvisibility(gone);
}
else
{
if (progressbar.getvisibility() == gone)
progressbar.setvisibility(visible);
progressbar.setprogress(newprogress);
}
super.onprogresschanged(view, newprogress);
}
// 处理javascript中的console.log
@override
public boolean onconsolemessage(consolemessage cm)
{
android.util.log.d(tag, "webview console " + cm.linenumber() + " of " + cm.sourceid() + " : " + cm.message());
return true;
} // 处理javascript中的alert()
@override
public boolean onjsalert(webview view, string url, string message, jsresult result)
{
result.cancel();
return true;
}
}
@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);
}}

3.mainactivity.java

?
1
2
3
4
5
6
7
8
progresswebview webview = (progresswebview)findviewbyid(r.id.them_webview);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("http://www.cnblogs.com/hubli/p/4835549.html");

4.activity_main.xml

?
1
2
3
4
<com.etoury.webviewprogress.progresswebview
android:id="@+id/them_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />

通过以上代码内容给大家介绍了android webview线性进度条的相关知识,希望对大家有所帮助。

延伸 · 阅读

精彩推荐