java - Android webview 启动画面不消失

标签 java android xml webview splash-screen

我遵循了此处关于将启动画面放在 web View 上的答案,我知道它现在出现在我的应用程序中,但现在它并没有像预期的那样消失。当我注释掉初始屏幕时,网站加载正常。

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>

最佳答案

请评论后查看
mWebView.setWebViewClient(new MyAppWebViewClient());

关于java - Android webview 启动画面不消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34629696/

相关文章:

android - 在Android上实现后台位置跟踪

php - 如何在另一个网站上显示Liferay数据库中存储的数据?

xml - XML header 中的 'encoding'有什么用?

java - 为什么要使用两种不同的算法对数组进行排序?

java - "INSTALL_PARSE_FAILED_MANIFEST_MALFORMED"在Android Studio中运行项目时出错

java - Java 中不同时区的日期格式

javascript - 是否可以通过 css 使选择下拉列表的选项显示为 optgroup

java - 从网络(Twitter)读取 JSON 数据

android - 试图找到一个android数据结构

android - clearColorFilter() 不适用于 android 2.3