【问题标题】:Splash screen on a Android WebView while loads first time until javascript onload fires首次加载时,Android WebView 上的启动画面,直到 javascript onload 触发
【发布时间】:2022-01-20 23:40:23
【问题描述】:

打开应用程序时所需的行为是:

  • 显示初始屏幕并并行加载 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_main.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 作为初始屏幕与应用程序的其余部分并行加载,然后,如何删除。

【问题讨论】:

    标签: javascript android


    【解决方案1】:

    似乎这是有效的,不确定是正确的方法:

    1. 修改 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 并且一些白体闪烁直到页面完全加载,但可以固定设置&lt;body style='background-color'&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-29
      • 2018-03-01
      • 1970-01-01
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      • 2018-07-16
      • 1970-01-01
      相关资源
      最近更新 更多