我正在使用WebViewClient加載網頁,我想爲我的代碼顯示進度條。我的代碼沒有逐漸加載進度。它只顯示100%的進展。如何根據網頁加載時間設置進度?使用ProgressBar顯示WebViewClient的進度
爲了我的目的,我只需在WebView上加載「https://www.google.com」。請幫忙。謝謝。代碼如下所示:
活動文件
public class MainActivity extends Activity {
private WebView webView;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int vc = Color.parseColor("#feca01");
progressBar = (ProgressBar) findViewById(R.id.determinateBar);
progressBar.getProgressDrawable().setColorFilter(
vc, android.graphics.PorterDuff.Mode.SRC_IN);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new MyBrowser());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://deelay.me/5000/http://www.google.com");
}
private class MyBrowser extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.incrementProgressBy(10);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.VISIBLE);
// progressBar.setProgress(0);
}
}
}
XML文件
`
<ProgressBar
android:id="@+id/determinateBar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="400dp"
android:padding="0dp"
android:layout_height="2dp" />
<WebView
android:layout_height="match_parent"
android:id="@+id/webView"
android:layout_weight="0.91"
android:layout_width="match_parent"
android:padding="0dp"
/>
`
參見[這](https://stackoverflow.com/questions/16849347 /添加-a-progress-dialog-in-a-webview)答案。 –
請參閱鏈接: https://stackoverflow.com/questions/25911185/set-progress-bar-progress-color-programatically並檢查它是否適合您的要求 –
@KunalChawla,它沒有工作。 : - \ – sairajreddy