首页 > 解决方案 > Webview 充气时不加载

问题描述

我正在开发一个带有可以显示多个屏幕的导航抽屉的应用程序。这些页面都显示在同一个页面中,Activity但通过将它们膨胀到包装器中。

<android.support.design.widget.CoordinatorLayout 
    //some parameters 

    <include
        android:id="@+id/main_container"
        layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

其中一个屏幕包含一个WebView.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/webview_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">


    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>

在 Webview 上,我附加了一个WebViewClient用 Javascript 处理一些 html 操作。

WebView webView = findViewById(R.id.web_view);
if (webView == null) {
    inflateLayout(R.layout.layout_with_webview);
    webView = findViewById(R.id.web_view);
}
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new CustomWebViewClient());
webView.loadUrl("http://www.somesite.com");

如果我将Activity 启动时WebView加载的布局放入加载中,则所有内容都会正确加载。setContentView()之后,我main_container使用以下代码将不同的 Layout 充气到:

public void inflateLayout(int toinflate) {
    ConstraintLayout mainLayout = findViewById(R.id.main_container);
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(toinflate, null);
    mainLayout.removeAllViews();
    mainLayout.addView(layout);
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
}

当我现在想膨胀一个包含任何内容的布局时,即使正在调用该方法,也会WebView在我调用时显示。webView.loadUrl("some url")onPageFinished(...)

现在的问题是:我做错了什么以及如何使用通过通货膨胀附加到屏幕的 WebView。

另外:我已经尝试使用添加 WebViewaddView并且它不起作用。

标签: androidwebviewandroid-inflate

解决方案


您需要重新初始化新的膨胀布局引用

public void inflateLayout(int toinflate) {
    ConstraintLayout mainLayout = findViewById(R.id.main_container);
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(toinflate, null);
    mainLayout.removeAllViews();
    mainLayout.addView(layout);

   // you need to reinitialise the web view which will refer to 
   // the web view in newly inflated layout as
   webView = findViewById(R.id.web_view);
   webView.getSettings().setJavaScriptEnabled(true);
   webView.setWebViewClient(new CustomWebViewClient());

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
}

因为新的相关视图与活动的现有布局没有链接,因此 Web 视图正常加载但不会对屏幕产生影响

通货膨胀是昂贵的,所以有效的选择是碎片


更新::根据约束布局,膨胀的布局必须具有适当的布局参数,因此请使用

View layout = inflater.inflate(toinflate, mainLayout);

推荐阅读