2016-01-06 55 views
1

我在這裏回答了關於在webview上放置啓動畫面的答案,我知道它現在出現在我的應用程序中,但現在它不像它應該的那樣是陰暗的。當我註釋出啓動畫面時,該網站加載得很好。Android webview啓動畫面不會消失

java文件 - onCreate是我認爲的興趣點。

package com.emman.app; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.ImageView; 
import android.widget.ProgressBar; 


public class MainActivity extends Activity { 

    private WebView mWebView; 
    ImageView imgLoading; 
    ProgressBar bar; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mWebView = (WebView) findViewById(R.id.activity_main_webview); 
     imgLoading = (ImageView)findViewById(R.id.imgloader); 
     bar = (ProgressBar) findViewById(R.id.progressBar1); 
     // Enable Javascript 
     WebSettings webSettings = mWebView.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 

     // Force links and redirects to open in the WebView instead of in a browser 
     mWebView.setWebViewClient(new WebViewClient() { 

      @Override 
      public void onPageFinished(WebView view, String url) { 
      // super.onPageFinished(view, url); commented out not fix 
       //hide loading image 
       imgLoading.setVisibility(View.GONE); 
       bar.setVisibility(View.GONE); 
       //show webview 
       mWebView.setVisibility(View.VISIBLE); 
      } 


     }); 

     // Use remote resource 
     mWebView.loadUrl("http://8ch.net"); 

     // Stop local links and redirects from opening in browser instead of WebView 
     mWebView.setWebViewClient(new MyAppWebViewClient()); 

     // Use local resource 
     // mWebView.loadUrl("file:///android_asset/www/index.html"); 
    } 

    // Prevent the back-button from closing the app 
    @Override 
    public void onBackPressed() { 
     if(mWebView.canGoBack()) { 
      mWebView.goBack(); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle item selection 
     switch (item.getItemId()) { 
      case R.id.action_settings: 
       finish(); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
     /* // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     }*/ 
    } 
} 

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"> 

    <ImageView 
     android:id="@+id/imgloader" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:src="@drawable/splash" 
     android:visibility="visible" /> 

    <ProgressBar 
     android:id="@+id/progressBar1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="101dp" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:visibility="visible"/> 

    <WebView 
     android:id="@+id/activity_main_webview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:visibility="gone" /> 

</RelativeLayout> 
+0

根據我的說法,您必須在主線程上啓動一個新線程,在其中顯示加載映像,並在主線程中加載該網站。 –

+0

另外,使您的進度條與網頁加載狀態同步 –

回答

3

請評論這
mWebView.setWebViewClient後(新MyAppWebViewClient());