【问题标题】:Unable to create layer for WebView with NestedScrollView无法使用 NestedScrollView 为 WebView 创建图层
【发布时间】:2019-06-10 14:50:48
【问题描述】:

我正在将 NestedscrollView 添加到 web 视图中,以便在用户滚动时隐藏/显示工具栏。在我添加 NestedScrollView 以进行隐藏/显示移动之前,该活动运行良好。在这一刻,我遇到了一个错误,我不知道如何解决它。

java.lang.IllegalStateException: Unable to create layer for WebView, size 720x12996 exceeds max size 8192

这是我的xml

 <androidx.coordinatorlayout.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <LinearLayout
            android:id="@+id/ly_webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@android:color/background_light">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="25dp"
                android:background="@color/negro">
            </RelativeLayout>

            <include layout="@layout/generic_toolbar_browser" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <androidx.core.widget.NestedScrollView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="fill_vertical"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior">
                    <WebView
                        android:id="@+id/webview_browser"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </androidx.core.widget.NestedScrollView>

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rv_web_items"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:visibility="gone"
                    android:layout_marginTop="@dimen/margin_standard"/>

            </RelativeLayout>
        </LinearLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

Generic_toolbar_browser

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/home_appbarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar_browser"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        app:layout_collapseMode="pin"
        app:titleTextColor="@color/white"
        app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:layout_scrollFlags="scroll|enterAlways"
        android:background="@color/blanco">

        .....

    </androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>

【问题讨论】:

  • 问题出在我活动中的这一行。删除后,一切正常 webview_browser.setLayerType(View.LAYER_TYPE_HARDWARE, null)
  • 我也遇到了同样的错误,在我的情况下,ScrollView 中有WebView,我想停止滚动WebView,所以我在 WebView 中使用了这个android:scrollbars="none"它是否适合我?请帮忙

标签: android webview android-webview android-nestedscrollview


【解决方案1】:

WebView 可以自行滚动。不必将其放在NestedScrollView 中。这样做可能会减小 WebView 的大小。不要担心app:layout_behavior="@string/appbar_scrolling_view_behavior",它也应该适用于WebView(尽管存在难以设置的边缘情况)。例如

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <WebView
            android:id="@+id/webview_browser"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_web_items"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:visibility="gone"
            android:layout_marginTop="@dimen/margin_standard"/>

    </RelativeLayout>

如果这不起作用,请查看 WebView 的父母。其中一个可能太大(=它的高度)。所以尽量不要超出屏幕的高度

【讨论】:

  • 是的,知道不会崩溃,但不会隐藏/显示工具栏。什么都不做。我已经用工具栏代码更新了我的问题
猜你喜欢
  • 2021-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多