android - 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
并且它不起作用。
解决方案
您需要重新初始化新的膨胀布局引用
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);
推荐阅读
- python - 将对象 dtype 列转换为 float 或 int
- python - 在 Python 中搜索和排序文件
- c# - Blazor route with encrypted parameters
- r - 用一个共同的图例绘制四个图
- php - 有没有办法让 file_puts_contents 独立工作并跳转到下一行?
- jmeter - 安排 JMeter 测试计划在某个时间开始
- python - Getting "object of type 'NoneType' has no len()" during implementation of mergeSort
- inno-setup - Inno 更新脚本,访问被拒绝...代码 5
- javascript - 如何阻止函数执行?
- javascript - 从 geojsons 动态创建的 svg 路径不会渲染到 ref