欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android WebView线性进度条实例详解

程序员文章站 2023-12-21 18:00:23
推荐阅读:android webview添加网页加载进度条实例详解 先给大家展示下效果图:这个效果图大家一看就懂,在生活经常见到 1.wevbview_progre...

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

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

Android WebView线性进度条实例详解

1.wevbview_progressbar.xml

<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

@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

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

<com.etoury.webviewprogress.progresswebview 
android:id="@+id/them_webview" 
android:layout_width="match_parent" 
android:layout_height="match_parent" />

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

上一篇:

下一篇: