打开应用程序时所需的行为是:
- 显示启动屏幕并并行加载URL
- 当javascript接口在加载时触发时,只需移除启动屏幕
Mainactivity.java
myWebView.addJavascriptInterface(new JavaScriptInterface(this, cookieManager),"Android");
JavaScriptInterface.java
@JavascriptInterface
public void hideOrRemoveSplashScreen() {
objetcSplashScreen.doRemoveSplashScreen();
//...
}
HTML页面(仅适用于加载了应用程序的页面,应使用用户代理检测(
$(function() {
try{Android.hideOrRemoveSplashScreen()}catch(e){};
});
Activity_min.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/pullfresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<WebView
android:id="@+id/msw_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"></WebView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
我不知道如何将一个简单的.png作为启动屏幕与应用程序的其他部分并行加载,然后,如何删除。
似乎这是有效的,不确定正确的方式:
- 修改activity_main.xml以便放置一个包含图像和webview的容器
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/pullfresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/splashimg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/darkblue"
android:src="@drawable/splash" />
<WebView
android:id="@+id/msw_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"></WebView>
</LinearLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
然后在MainActivity.java中添加对象
container = findViewById(R.id.container);
splash = findViewById(R.id.splashimg);
并最终删除Oncreate 内加载的图像
myWebView.setWebChromeClient(new MyChrome() {
[...]
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100){
container.removeView(splash);
}
super.onProgressChanged(view, newProgress);
}
}
加载第一页时,会触发removeView,一些白色主体会闪烁,直到页面完全加载,但可以修复设置<body style='background-color'>