我是编程新手。我正在尝试创建一个带有Webview/Android的应用程序。我正在尝试添加一种类型的启动图像,直到网页加载完成。我得到了开机画面,但在url加载完成之前,极乐世界就结束了。然后,在页面显示之前,我会看到一个白屏。我尝试了许多不同的指南,但都不能解决问题。我希望有人能帮我做这件事。
我想要飞溅或图像显示,而网页完成加载和显示页面之间没有任何白屏。
下面是我的代码:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
MainActivity.java
package com.eatnow.brasil
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.graphics.Bitmap;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//webview use to call own site
webView =(WebView)findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient());//use to hide the address bar
webView .getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true); // Enable zoom on sites
webView.getSettings().setSupportZoom(true);
webView .getSettings().setTextZoom(95); // where 90 is 90%; default value is ... 100
webView .getSettings().setDomStorageEnabled(true); //to store history
webView.setBackgroundColor(0xff2f0848);
//imporove webView performance
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setSaveFormData(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl("https://eat-now.no/");
}
@Override
public void onBackPressed() {
if(webView.canGoBack())
{
webView.goBack();
}
else{
super.onBackPressed();
}
}
}
activity_splash.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#2f0848"
tools:context=".SplashActivity">
<ImageView
android:id="@+id/splash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:contentDescription="@string/app_name"
android:src="@drawable/eatnow_splash" />
</RelativeLayout>
发布于 2021-01-25 10:17:55
这是WebView的一个典型问题。当加载webView时,我们通常使用ProgressDiglog来填充白屏。
下面是一个在WebView存储时使用ProgressDialog的示例:https://github.com/rupok/webview
这是一个使用eclipse IDE的老项目,但您可以在布局中引用ShowWebView.java和xml。如果您愿意,您可以使用图像来替换ProgressDialog。
https://stackoverflow.com/questions/65876625
复制相似问题