星空网 > 软件开发 > 操作系统

WebView线性进度条

参考:http://www.cnblogs.com/hubli/p/4835549.html

效果图:

WebView线性进度条images/loading.gif' data-original="http://images2015.cnblogs.com/blog/771964/201601/771964-20160119191929562-954829741.png" />

1.wevbview_progressbar.

<layer-list 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.

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



原标题:WebView线性进度条

关键词:web

web
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

运输物流行业:https://www.goluckyvip.com/tag/12169.html
Q1出货破1亿:https://www.goluckyvip.com/tag/1217.html
海外KOC:https://www.goluckyvip.com/tag/12171.html
海外意见领袖:https://www.goluckyvip.com/tag/12172.html
跨境人日常:https://www.goluckyvip.com/tag/12173.html
疫情拐点:https://www.goluckyvip.com/tag/12174.html
What?我的银行账户因为涉嫌洗钱被关闭?!:https://www.xlkjsw.com/news/36579.html
亚马逊与亚洲首富battle!这一站点卖家笑了...:https://www.xlkjsw.com/news/36580.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流