在Android中的Webview之前避免使用白屏

时间:2015-12-08 03:30:59

标签: java android webview delayed-execution

我正在开发一个Android应用程序,我有两个活动启动和第一个。我正在webview中包装一个web url。但每当我点击webview中的任何链接时,都会出现白屏。如何删除白色屏幕,因为它给最终用户带来了非常烦人的体验。以下是我的代码

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.admin.novjan">
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".first"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">

        </activity>
        <activity
            android:name=".splash"
            android:label="@string/title_activity_splash"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
    </application>

</manifest>

Splash.java

package com.example.admin.novjan;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class splash extends AppCompatActivity {
    // Splash screen timer
    private static int SPLASH_TIME_OUT = 5000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        new Handler().postDelayed(new Runnable() {

                @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity
                Intent i = new Intent(splash.this, first.class);
                startActivity(i);

                // close this activity
                finish();
            }
        }, SPLASH_TIME_OUT);
    }

}

first.java

package com.example.admin.novjan;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class first extends AppCompatActivity {


    private WebView wv1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        // set content view
        setContentView(R.layout.activity_first);
        //set web view
        wv1=(WebView)findViewById(R.id.webView);

        //wv1.setWebViewClient(new MyWebViewClient());
        wv1.setWebViewClient(new MyWebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                findViewById(R.id.progressBar1).setVisibility(View.VISIBLE);
                wv1.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                findViewById(R.id.progressBar1).setVisibility(View.GONE);

                findViewById(R.id.webView).setVisibility(View.VISIBLE);


            }
        });
        String url = "http://novjan.com/mhome/";
        wv1.getSettings().setJavaScriptEnabled(true);
        wv1.loadUrl(url);
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}//end of first class

content_first.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.admin.novjan.first"
    tools:showIn="@layout/activity_first">

    <WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/webView"
        />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="false"
        android:layout_centerInParent="true" />
</LinearLayout>

1 个答案:

答案 0 :(得分:2)

显示进度条:

public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (m_webView.getProgress() == 100) {
                progressBar.setVisibility(View.GONE);
                m_webView.setVisibility(View.VISIBLE);
            }
        } 

显示并从您的onCreate或您认为可行的任何其他方法开始进度。