发布于 2016-02-17 15:16:07 | 136 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了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线性进度条的相关知识,希望对大家有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务