【问题标题】:How to remove the white screen before the webview load?如何在 webview 加载之前删除白屏?
【发布时间】:2017-02-03 02:27:37
【问题描述】:

我正在创建一个应用程序,该应用程序通过webview 加载网站,在它显示启动屏幕之前。问题是在启动画面之后会出现一个白屏,然后会加载 webview
我不想在启动画面中使用计时器,我希望它在 webview 加载后消失。我看到我需要将启动活动移动到主要活动,但我不确定如何。我是 android studio 的初学者。

AndroidManifest.xml:

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>

        <activity
            android:name=".SplashActivity"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />

        <activity
            android:name=".MainActivity"
            android:theme="@style/Theme.AppCompat.NoActionBar">
        </activity>
    </application>

</manifest>

MainActivity.java:

package app.atlasdatabase;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import static app.atlasdatabase.R.style.AppCompatAlertDialogStyle;


public class MainActivity extends AppCompatActivity {

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

        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        WebView myWebView = (WebView) findViewById(R.id.atlasdatabase);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        myWebView.loadUrl("file:///android_asset/index.html");

    }

    /**
     * Exit the app if user select yes.
     */
    private void doExit() {

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                MainActivity.this, AppCompatAlertDialogStyle);

         alertDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });

        alertDialog.setTitle(R.string.exit);
        alertDialog.setMessage(R.string.exitmsg);
        alertDialog.setNegativeButton(R.string.no, null);
        alertDialog.show();
    }

    @Override
    public void onBackPressed()
    {
        WebView webView = (WebView) findViewById(R.id.atlasdatabase);
        if(webView.canGoBack()){
            webView.goBack();
        }else{
            /* Close the app without the Dialog
            super.onBackPressed();
             */
            /* Use the dialog to Exit the App */
            doExit();
        }
    }
}

SplashActivity.java:

package app.atlasdatabase;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    tools:context="atlasdb.atlasdatabase.MainActivity">

    <WebView
        android:id="@+id/atlasdatabase"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/banner_home_footer">
    </com.google.android.gms.ads.AdView>

</RelativeLayout>

background_splash.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Background color -->
    <item android:drawable="@color/colorPrimary"/>

    <!-- Image at the center of the screen -->
    <item>
        <bitmap android:gravity="center" android:src="@drawable/splash"/>
    </item>
</layer-list>

【问题讨论】:

  • 好吧,让我们保持干净,我也是这样做的。
  • 如果你还有问题,你可以问我那天我不像今天那样忙
  • 好的,谢谢,我在那里给你发了一条消息。我会在这里再试一次,如果我成功了,我会通知你。再次感谢:)

标签: android xml webview


【解决方案1】:
  • 在答案中,您可以看到需要关注的 cmets。
  • 您不需要SplashActivity
  • 您可以将 match_parent 的视图保留到根视图(因此它将全屏显示)并向其中添加您的图像或其他任何内容。
  • 当您的 Activity 加载时,请确保它在给定的代码中可见/或类似 @onPageStarted 如果视图尚未加载,您可以显示您的徽标..
  • 当 web 视图准备就绪时,如评论 @onPageFinished 中所示,使您的 splashView 不可见或视图消失!

对于这些任务,您可以在 Xml 中使用 android:visibility="gone"yourRootViewWithImage.setVisibility(View.VISIBLE); yourRootViewWithImage.setVisibility(View.GONE); 在适当的地方使用这些行!

归功于this 帖子,示例:

   public class MainActivity  extends AppCompatActivity  {


    private boolean loadingFinished = true;
    private boolean redirect = false;
    private WebView myWebView;

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

        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        myWebView = (WebView) findViewById(R.id.atlasdatabase);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        myWebView.loadUrl("file:///android_asset/index.html");


        myWebView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(
                    WebView view, WebResourceRequest request) {
                if (!loadingFinished) {
                    redirect = true;
                }

                loadingFinished = false;
                myWebView.loadUrl(request.getUrl().toString());
                return true;
            }

            @Override
            public void onPageStarted(
                    WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                loadingFinished = false;
                //SHOW LOADING IF IT ISNT ALREADY VISIBLE
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                if(!redirect){
                    loadingFinished = true;
                }

                if(loadingFinished && !redirect){
                    //HIDE LOADING IT HAS FINISHED // HIDE YOUR SPLASH/LOADING VIEW
                } else{
                    redirect = false;
                }
            }
        });

    }
/// below code ..

}

【讨论】:

  • 好的,但是如何在我的代码上实现它而不弄乱它?
  • @RogerHN 很容易你有另一个布局作为根或第一个孩子。我认为你可以很容易地做到这一点。然后将你的图像添加到该布局中。确保它会在你想要的时候显示加载你的第一个任务的活动
  • @RogerHN 那么最简单的方法是在您的 onCreate 中添加此代码,如果您有问题请告诉我
  • 不确定我是否理解如何操作。你能在我的代码中给我看一个例子吗?我只需要将该代码添加到我的 MainActivity.java onCreate 部分吗?以及如何将图像移动到您提到的布局?对不起,我还在学习。我用教程完成了大部分工作:)
  • @RogerHN 更新了答案,但您需要添加一些您拥有的方法,但 onCreate 部分是否存在我的完整答案不会帮助您了解这件事
【解决方案2】:

我找到了最简单的答案 How to manage the Blank White Loading screen of an Android Webview?,它帮助我解决了我在没有任何 Splash 活动屏幕的情况下面临的问题:

在 Android 清单中:

    <activity android:name=".MainActivity"
        android:theme="@style/Splash">
...

在Activity上,创建webView后,设置背景为透明。

myWebView = (WebView)findViewById(R.id.webView);
myWebView.setBackgroundColor(Color.TRANSPARENT);

(我在 pageFinished 上添加了进度条弹出窗口并终止,以便用户知道加载正在进行中。)

【讨论】:

    【解决方案3】:

    我以前遇到过这个问题,这只是一个 hack,但我遇到了同样的问题,出于某种原因,我仍在阅读原因,但是将 web 视图从片段打开更改为活动导致白色开始加载 Web 视图时的屏幕消失了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-29
      • 2016-08-30
      • 1970-01-01
      • 2015-07-13
      相关资源
      最近更新 更多