Webview 膨胀时不加载
Webview not loading when inflated
我正在开发一个带有可以显示多个屏幕的导航抽屉的应用程序。这些页面都显示在同一个 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");
如果我将 WebView
放入用 setContentView()
加载的布局中,当 activity 启动时一切都正确加载。之后,我使用以下代码将不同的布局膨胀到 main_container
中:
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
的布局时,即使正在调用 onPageFinished(...)
方法,调用 webView.loadUrl("some url")
时也没有显示任何内容。
现在的问题是:我做错了什么以及如何使用 inflation.
附加到屏幕的 WebView
另外:我已经尝试使用 addView
添加 WebView 但它没有用。
您需要重新初始化新的扩充布局引用
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);
}
因为新的相关视图没有 link 与 activity 的现有布局,因此网络视图可以正常加载,但不会对屏幕产生影响
Inflation 很昂贵,所以有效的选择是 Fragments
更新::膨胀布局必须根据约束布局具有适当的布局参数,因此使用
View layout = inflater.inflate(toinflate, mainLayout);
<?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>
父布局是 ConstraintLayout,而子组件即 webView 并不真正具有有效属性。首先在预览中检查这个布局,你能在其中看到你的 webview 吗?
如果不试试这些-
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
我正在开发一个带有可以显示多个屏幕的导航抽屉的应用程序。这些页面都显示在同一个 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");
如果我将 WebView
放入用 setContentView()
加载的布局中,当 activity 启动时一切都正确加载。之后,我使用以下代码将不同的布局膨胀到 main_container
中:
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
的布局时,即使正在调用 onPageFinished(...)
方法,调用 webView.loadUrl("some url")
时也没有显示任何内容。
现在的问题是:我做错了什么以及如何使用 inflation.
附加到屏幕的 WebView另外:我已经尝试使用 addView
添加 WebView 但它没有用。
您需要重新初始化新的扩充布局引用
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);
}
因为新的相关视图没有 link 与 activity 的现有布局,因此网络视图可以正常加载,但不会对屏幕产生影响
Inflation 很昂贵,所以有效的选择是 Fragments
更新::膨胀布局必须根据约束布局具有适当的布局参数,因此使用
View layout = inflater.inflate(toinflate, mainLayout);
<?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>
父布局是 ConstraintLayout,而子组件即 webView 并不真正具有有效属性。首先在预览中检查这个布局,你能在其中看到你的 webview 吗? 如果不试试这些-
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>