如何在Webview Android Studio中添加带有进度条的启动画面

时间:2016-12-19 06:47:42

标签: android webview progress-bar android-webview

如何在Webview android studio中添加带有进度条的启动画面

我是android开发的新手,帮我添加带有进度条的启动画面

activity_main.xml中:

    <?xml version="1.0" encoding="utf-8"?>
    <Relati veLayoutxmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="0dp"
        android:paddingLeft="0dp"
        android:paddingRight="0dp"
        android:paddingTop="0dp"
        tools:context="com.example.tamil.stagingsite.MainActivity">



        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="34dp"
            android:background="@mipmap/ic_launcher" />
        <WebView
            android:id="@+id/activity_main_webview"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:animationCache="true"
            android:background="@android:color/white"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true">

        </WebView>

        <ProgressBar
            style="@android:style/Widget.Material.Light.ProgressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:id="@+id/progressBar2" />
    </RelativeLayout>

主要活动.java:

在主要活动java文件中我添加了以下内容

package com.example.tamil.stagingsite;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

import static com.example.tamil.stagingsite.R.id.webview;

public class MainActivity extends Activity {
    WebView webView;
    ProgressBar bar;


    private WebView mWebView;


    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        bar=(ProgressBar) findViewById(R.id.progressBar2);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("https://census-staging-pr-26.herokuapp.com");
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.setWebViewClient(new MyAppWebViewClient());


        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

    }


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


    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("Main Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("https://census-staging-pr-26.herokuapp.com"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }
    public class MyAppWebViewClient extends WebViewClient{
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            bar.setVisibility(View.GONE);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return super.shouldOverrideUrlLoading(view, url);
        }

    }




}

2 个答案:

答案 0 :(得分:0)

您可以查看此链接progressbar in webview

答案 1 :(得分:0)

在主文件夹中创建一个资源文件夹。

为进度条添加一个gif,然后添加一个html文件添加图片标记并传递如下所示的gif链接:

<img src="rays.gif" alt="wait..." ><br>

在表单标记中,您可以将链接显示在webview上

以下是完整的HTML文件:

<!DOCTYPE html>
<html>
<body onload="document.getElementById('registerform').submit();">
<center>
    <img src="getImage.gif" alt="wait..." ><br>
    <h4 style="font-family: Helvetica;">Please wait while you are being redirected</h4><br>
    <img src="rays.gif" alt="wait..." ><br>
    <form id="form"  action="yourLink"
          method="POST">
        [PLACEHOLDER]
    </form>

</center>
</body>
</html>

希望这会有所帮助。