【问题标题】:Android Webview : Splash screen while loading a urlAndroid Webview:加载网址时的闪屏
【发布时间】:2014-10-27 06:59:02
【问题描述】:

我是 Android 应用程序的新手,这是我的第一个应用程序。 我已经创建了启动画面并且可以工作..但是在它消失后大约 2-5 秒出现一个长长的白色空白屏幕然后 url 开始加载..

我的问题是.. 加载 URL 时如何在启动画面中制作一些加载或进度条?所以没有更多的白色空白屏幕。对不起我的英语不好。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx.xxx" >

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="xxx"
    android:theme="@style/AppTheme"
    android:hardwareAccelerated="true" />


    <activity
        android:name="com.xxx.xxx.Splash"
        android:label="xxx"
        android:theme="@style/Theme.MyAppTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MyActivity"
        android:hardwareAccelerated="true"
        android:configChanges="orientation|screenSize"
        android:label="xxx" >

    </activity>

</application>

</manifest>

activity_my.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MyActivity">

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

<TextView
    android:id="@+id/display"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone"
     />

<FrameLayout
        android:id="@+id/customViewContainer"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone"/>
</LinearLayout>    

Splash.java

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;

public class Splash extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splashscreen);

    final Handler handel = new Handler();
    handel.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            Intent loadSplash = new Intent(Splash.this, MyActivity.class);

            startActivity(loadSplash);

            finish();
            }
        }, 3000);
    }
}

splashscreen.xml

<?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:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >

<ImageView
android:id="@+id/imageView1"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/splash" />

</RelativeLayout>

【问题讨论】:

标签: java android webview loading splash-screen


【解决方案1】:

不要将启动画面创建为单独的活动。在您的 WebView 活动上添加启动功能,即您的页面加载活动。监听onPageFinished 事件并隐藏启动图像或动画。

mWebView.setWebViewClient(new WebViewClient() {

   public void onPageFinished(WebView view, String url) {
        //hide splash functionality
    }
});

如果你想显示一个进度条,你也可以得到页面加载进度

mWebView.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress) 
                   {
                   if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){
                       Pbar.setVisibility(ProgressBar.VISIBLE);
                   }
                   Pbar.setProgress(progress);
                   if(progress == 100) {
                       Pbar.setVisibility(ProgressBar.GONE);
                   }
                }
            });

【讨论】:

  • 这是您的进度条,所以通过代码或在 xml 中创建一个进度条以显示在您的活动中并在 onProgressChanged 事件中更新
【解决方案2】:

尝试这两个选项中的任何一个。希望对您有所帮助

1.删除处理程序的回调

handel.postDelayed(new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        Intent loadSplash = new Intent(Splash.this, MyActivity.class);

        startActivity(loadSplash);

        finish();
        handel.removeCallbacks(this);

        }
    }, 3000);
  1. 使用线程而不是处理程序

    public class Splash extends Activity {
    
    protected boolean _active = true;
    protected int _splashTime = 3000;
    Thread splashTread;
    private boolean stop = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashscreen);
        splashTread = new Thread() 
        {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while (_active && (waited < _splashTime)) {
                        sleep(100);
                        if (_active) {
                            waited += 100;
                        }
                    }
                } catch (InterruptedException e) {
                    // do nothing
                } finally {
                    if (!stop) {
                        startActivity(new Intent(Splash.this, MyActivity.class));
                        finish();
                    } else
                        finish();
                }
            }
        };
        splashTread.start();
    }
    

    }

【讨论】:

  • 我尝试了第二个代码..启动画面工作,之后..长长的白色空白屏幕..
  • 如果它工作,请将其标记为答案它也会对其他人有所帮助,或者如果您更改任何代码,请同时发布
【解决方案3】:

这是怎么做的-

    wv.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            //hide loading image
            findViewById(R.id.imageLoading1).setVisibility(View.GONE);
            //show webview
            findViewById(R.id.webView1).setVisibility(View.VISIBLE);
        }
    });     

    wv.loadUrl("http://yoururlhere.com");

您可以在此处找到完整示例 - Splash Screen While Loading a URL

【讨论】:

    【解决方案4】:

    您可以使用 AsyncTask 并将加载 URL 代码放入 AsyncTask 并使用 进度条 来查看进度。

    帮助链接:

    For AsyncTask

    For ProgressBar

    希望对你有所帮助。

    【讨论】:

      【解决方案5】:

      给你.. 正如Jibran Khan 所说,不要创建单独的启动活动。在您的 webview 上使用启动功能。首先通过扩展为WebViewClient创建一个webclient类,你会得到像这样的未实现的方法`

      {
          @Override
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
              view.loadUrl(url);
              return true;
          }
      
          @Override
          public void onLoadResource(WebView view, String url) {
      
          }
      
          public void onPageStarted(WebView view, String url, Bitmap favicon) {
              progressBar1.setVisibility(View.VISIBLE);
              webview.setVisibility(View.GONE);
              super.onPageStarted(view, url, favicon);
          }
      
          public void onPageFinished(WebView view, String url) {
              progressBar1.setVisibility(View.GONE);
              webview.setVisibility(View.VISIBLE);
          }
      }
      

      ` 使用进度条作为加载指示器,如上面代码中所做的那样。

      不要忘记这样做web.setWebViewClient(new MyWebViewClient());,其中MyWebViewClient 是您的网络客户端类名

      这是上面代码的xml

       <WebView
          android:id="@+id/web"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"
          android:visibility="gone" />
      
      <ProgressBar
          android:id="@+id/progressBar1"
          style="?android:attr/progressBarStyleLarge"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true" />
      

      【讨论】:

      • 我把xml放在上面得到了Cannot Resolve Symbol progressBar1 ?
      • 在你的类中声明进度条为 ProgressBar progressBar1;然后progressBar1 = (ProgressBar)findViewById(R.id.ProgressBar);
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-21
      • 2016-02-01
      • 1970-01-01
      相关资源
      最近更新 更多