我刚刚在导航抽屉中实现了webview。我尝试在下面的代码中实现进度条,但无法弄清楚如何在下面的代码中执行它。我阅读了关于webview和进度条的android文档和其他问题,这对我没有帮助。
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
WebView myWebView = (WebView) findViewById(R.id.web);
myWebView.loadUrl("http://www.google.com");
WebView WebView = (WebView) findViewById(R.id.web);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
WebView Webview = (WebView) findViewById(R.id.web);
myWebView.setWebViewClient(new WebViewClient());
if (myWebView.getProgress()>=100){
setProgressBarIndeterminate(false);
else{
setProgressBarIndeterminate(true);
}
我是android编程新手
答案 0 :(得分:0)
以下代码可以正常显示预加载器
主要活动
package com.android_examples.com.webviewprogressbarload;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ProgressBar;
public class MainActivity extends Activity
{
WebView webview;
ProgressBar pbar;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView)findViewById(R.id.webView1);
pbar = (ProgressBar)findViewById(R.id.progressBar1);
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("http://www.google.com");
}
public class WebViewClient extends android.webkit.WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url)
{
// TODO Auto-generated method stub
super.onPageFinished(view, url);
pbar.setVisibility(View.GONE);
}
} }
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.android_examples.com.webviewprogressbarload.MainActivity" >
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/progressBar1"
android:layout_centerHorizontal="true" />
</RelativeLayout>