如何在WebView Android中创建Progresbar

时间:2019-03-24 14:09:11

标签: java android android-layout android-webview

我尝试了很多方法,但是我不想要做的就是在加载网页时显示进度栏 请帮我这是我的代码 MainActivity.Java

package com.krishnakingofrestoration.progress;

 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.webview);
    pbar = (ProgressBar)findViewById(R.id.progressBar2);
    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);

    }

}

@Override
public void onBackPressed() {
    if (webview.canGoBack()){
        webview.goBack();
    }
    else {
        super.onBackPressed();
    }
}

activity_main.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"
  tools:context=".MainActivity" >

<ProgressBar
    android:id="@+id/progressBar2"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="217dp" />

<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="0dp" />
  </RelativeLayout>

我无法使用 Android Studio 3.2.1 和Android Marshmallow 6.0,请帮帮我Gradle 4.1.2 它是我的完整代码,我想您会帮助我 我想要圈子进度栏,请给我正确答案

2 个答案:

答案 0 :(得分:0)

您的ProgressBar当前隐藏在您的WebView下。只需更改.xml文件中的顺序

答案 1 :(得分:0)

首先像下面的代码那样在xml中交换WebView和ProgressBar的顺序:

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity" >

   <WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="0dp" />
   <ProgressBar
    android:id="@+id/progressBar2"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="217dp"/>
</RelativeLayout>

Java文件中的代码

public class MainActivity extends AppCompatActivity {
WebView webview;
ProgressBar pbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webview = findViewById(R.id.webview);
    pbar = findViewById(R.id.progressBar2);
    webview.setWebViewClient(new WebViewClient() {
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            pbar.setVisibility(View.VISIBLE);
        }
        public void onPageFinished(WebView view, String url) {

            super.onPageFinished(view, url);
            pbar.setVisibility(View.GONE);
        }
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            Toast.makeText(MainActivity.this,
                    "The Requested Page Does Not Exist", Toast.LENGTH_LONG).show();
            webview.loadUrl("https://www.google.com");
        }
    });
    webview.loadUrl("https://www.google.com");
    webview.getSettings().setLoadWithOverviewMode(true);
    webview.getSettings().setUseWideViewPort(true);
    webview.getSettings().setJavaScriptEnabled(true);
}
@Override
public void onBackPressed() {
    if (webview.canGoBack()){
        webview.goBack();
    }
    else {
        super.onBackPressed();
    }
  }
}

我希望它对您有用。