我正在创建一个应用程序,通过webview
加载网站,然后显示启动画面。问题是在启动画面后出现白色屏幕,然后webview
加载
我不想在启动scree中使用计时器,我希望它在加载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>
答案 0 :(得分:2)
SplashActivity
。match_parent
到根视图的视图(因此它将全屏显示)并添加您的图像或其他任何内容。onPageStarted
给定的代码中可见/或类似,您可以显示您的徽标.. 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 ..
}
答案 1 :(得分:0)
我以前曾遇到过这个问题,但这只是一个hack,但是我遇到了同样的问题,由于某些原因,我仍在阅读为什么将网页视图从打开状态更改为活动状态会导致白色加载Web视图开始时的屏幕消失了。
答案 2 :(得分:0)
我找到了最简单的答案 How to manage the Blank White Loading screen of an Android Webview?,它帮助我解决了没有任何启动活动屏幕时遇到的问题:
在Android清单中:
<activity android:name=".MainActivity"
android:theme="@style/Splash">
...
并在Activity上创建webView之后,将背景设置为透明。
myWebView = (WebView)findViewById(R.id.webView);
myWebView.setBackgroundColor(Color.TRANSPARENT);
(我添加了进度条弹出窗口,并在pageFinish上终止了,以便用户知道正在加载。)