Android WebView線性進度條實例詳解
更新時間:2016年01月20日 09:43:22 作者:路邊橋涼
這篇文章主要介紹了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線性進度條的相關知識,希望對大家有所幫助。
相關文章
Android 通過httppost上傳文本文件到服務器的實例代碼
這篇文章主要介紹了Android 通過httppost上傳文本文件到服務器的實例代碼,非常簡單易懂,非常實用,需要的朋友可以參考下2016-08-08
Android ListView與getView調用卡頓問題解決辦法
這篇文章主要介紹了Android ListView與getView調用卡頓問題解決辦法的相關資料,這里提供實例及解決辦法幫助大家解決這種問題,需要的朋友可以參考下2017-08-08
Android應用中設置alpha值來制作透明與漸變效果的實例
這篇文章主要介紹了Android應用中設置alpha值來制作透明與漸變效果的實例,展示了基礎的透明漸變動畫的編寫方法,需要的朋友可以參考下2016-04-04
Android之使用Android-query框架開發(fā)實戰(zhàn)(一)
這篇文章主要介紹了Android之使用Android-query框架開發(fā)實戰(zhàn)(一)的相關資料,需要的朋友可以參考下2015-10-10
淺談Android實踐之ScrollView中滑動沖突處理解決方案
涉及到了ViewPager,MapView,ListView,就需要ScrollView來做一下支援,這篇文章主要介紹了淺談Android實踐之ScrollView中滑動沖突處理解決方案,有需要的可以來了解一下。2016-12-12

