您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Android如何實(shí)現(xiàn)帶進(jìn)度條的WebView,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
Android 實(shí)現(xiàn)帶進(jìn)度條的WebView的實(shí)例
1. WebView加載網(wǎng)頁(yè)方法
//加載本地資源 loadUrl("file:///android_asset/example.html"); //加載網(wǎng)絡(luò)資源 loadUrl("http://baidu.com");
2. 帶進(jìn)度的Drawable文件view_progress_webview
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/progress"> <clip> <shape> <solid android:color="#31CE15"/> <corners android:radius="2dp"/> </shape> </clip> </item> </layer-list>
顏色值為進(jìn)度顏色,根據(jù)需要更換
3. ProgressWebView類
/** * 帶進(jìn)度條的WebView * @Author GQ */ public class ProgressWebView extends WebView { private ProgressBar progressbar; private Context mContext; public ProgressWebView(Context context, AttributeSet attrs) { super(context, attrs); this.mContext = context; progressbar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal); progressbar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 5, 0, 0)); Drawable drawable = context.getResources().getDrawable(R.drawable.view_progress_webview); progressbar.setProgressDrawable(drawable); addView(progressbar); //主要處理解析,渲染網(wǎng)頁(yè)等瀏覽器做的事情 setWebViewClient(new WebViewClient() { @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); //加載失敗時(shí)候,顯示自定義的頁(yè)面 if (errorListener != null) { errorListener.onError(); } } }); //輔助WebView處理Javascript的對(duì)話框,網(wǎng)站圖標(biāo),網(wǎng)站title,加載進(jìn)度等 setWebChromeClient(new WebChromeClient()); getSettings().setSupportZoom(true);//是否可以縮放 getSettings().setBuiltInZoomControls(true); getSettings().setJavaScriptEnabled(true);//支持JS getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS); getSettings().setUseWideViewPort(true); getSettings().setLoadWithOverviewMode(true); getSettings().setSaveFormData(true); getSettings().setDomStorageEnabled(true); //優(yōu)先使用緩存 getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //禁用長(zhǎng)按 setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View view) { return true; } }); //如果在瀏覽器下載,調(diào)用瀏覽器默認(rèn)下載+通知欄 setDownloadListener(new DownloadListener() { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { Uri uri = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW, uri); mContext.startActivity(intent); } }); } public class WebChromeClient extends android.webkit.WebChromeClient { @Override public void onProgressChanged(WebView view, int newProgress) { if (newProgress == 100) { progressbar.setVisibility(GONE); } else { if (progressbar.getVisibility() == GONE) progressbar.setVisibility(VISIBLE); progressbar.setProgress(newProgress); } super.onProgressChanged(view, newProgress); } @Override public void onReceivedTitle(WebView view, String title) { super.onReceivedTitle(view, title); if (titleListener != null) titleListener.getTitle(title); } } @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); } private TitleListener titleListener; public interface TitleListener { void getTitle(String title); } public void setOnTitleListener(TitleListener titleListener) { this.titleListener = titleListener; } private ErrorListener errorListener; public interface ErrorListener { void onError(); } public void setOnErrorListener(ErrorListener errorListener) { this.errorListener = errorListener; } }
4. 使用
/** * 公共WebView */ public class BasicWebActivity extends Activity { protected ProgressWebView progressWebView; private TextView title;//標(biāo)題欄 private TextView tv_none;//加載失敗顯示文字 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.common_webview); title = (TextView) findViewById(R.id.title); tv_none = (TextView) findViewById(R.id.tv_none); progressWebView = (ProgressWebView) findViewById(R.id.progressWebView); String url = getIntent().getStringExtra("url"); progressWebView.setOnTitleListener(new ProgressWebView.TitleListener() { @Override public void getTitle(String title) { title.setText(title); } }); progressWebView.setOnErrorListener(new ProgressWebView.ErrorListener() { @Override public void onError() { tv_none.setText("url資源失效"); } }); //加載網(wǎng)頁(yè) progressWebView.loadUrl(url); } //重寫(xiě)返回鍵 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (progressWebView.canGoBack()) { progressWebView.goBack();//返回上一層頁(yè)面 return true; } else { finish();//關(guān)閉頁(yè)面 } } return super.onKeyDown(keyCode, event); } }
其中common_webview就包含一個(gè)title一個(gè)progressWebView就不貼代碼了。
Android是一種基于Linux內(nèi)核的自由及開(kāi)放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開(kāi)放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開(kāi)發(fā)。
關(guān)于“Android如何實(shí)現(xiàn)帶進(jìn)度條的WebView”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。