我在progressbar
内的webview
中有一个fragment
。在onViewCreated()
方法中,我有:
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
progressbar
的定义如下:
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
/>
为了显示加载页面的进度,我有以下代码:
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
});
我正在加载网址:"http://www.techcrunch.com"
问题是,在加载页面时,progressbar
不可见。所以为了调试这个我试着在onProgressChanged()
方法中输出进度。我哪里错了?
答案 0 :(得分:1)
Custom Progress Drawable
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Define the background properties like color etc -->
<item android:id="@android:id/background">
<shape>
<gradient
android:startColor="#000001"
android:centerColor="#0b131e"
android:centerY="1.0"
android:endColor="#0d1522"
android:angle="270"
/>
</shape>
</item>
<!-- Define the progress properties like start color, end color etc -->
<item android:id="@android:id/progress">
<clip>
<shape>
<gradient
android:startColor="#007A00"
android:centerColor="#007A00"
android:centerY="1.0"
android:endColor="#06101d"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>
初始化您的进度条和网页浏览
webView = (WebView) rootView.findViewById(R.id.webView);
webView.setWebChromeClient(new MyWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
mprogressbar = (ProgressBar) rootView.findViewById(R.id.progressBar);
Drawable customDrawable= getResources().getDrawable(R.drawable.custom_progressbar);
mprogressbar.setProgressDrawable(customDrawable);
然后,在WebViewClient中设置进度,
private class MyWebViewClient extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
setValue(newProgress);
super.onProgressChanged(view, newProgress);
}
}
public void setValue(int progress) {
this.mprogressbar.setProgress(progress);
if(progress >= 100)
{
mprogressbar.setVisibility(View.INVISIBLE);
}
}