我希望在WebView应用程序正在加载背景主页时显示启动画面。 这就是我所做的:
mainactivity.java
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_WRITE_EXTERNAL_STORAGE = 0;
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
// hide splash
findViewById(R.id.activity_splash).setVisibility(View.GONE);
// show webview
mWebView.setVisibility(View.VISIBLE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
});
mWebView.loadUrl("http://www.example.com/");
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
// Prevent the back-button from closing the app
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
mainactivity.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:background="@color/colorPrimary"
tools:context="com.myapp.MainActivity">
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<ImageView
android:id="@+id/activity_splash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/splash_logo"
android:visibility="visible" />
</RelativeLayout>
但是这段代码显示启动画面显示时间不明显...屏幕保持白色一秒钟然后页面加载... 如何解决这个问题,任何更好的解决方案都会非常有用。谢谢:))