webview的高度在nestedScrollView内无限增长 [英] webview height grows indefinitely inside a nestedScrollView

本文介绍了webview的高度在nestedScrollView内无限增长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的布局.当我加载google.com时,网络视图的高度会无限期地增长. WebView的onSizeChange函数不断被调用,我可以看到WebView无限期地扩展.我已经尝试了设计库和appcompat库的22.2.1和23.1.0,并且没有任何效果.

This is my layout. When i load google.com, the webview's height keeps growing indefinitely. The onSizeChange function of the webview keeps getting called and i can see the webview keeps expanding indefinitely. I've tried 22.2.1 and 23.1.0 of the design and appcompat libraries and no effect.

任何解决方案? :-|

Any solution ? :-|

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">

<android.support.v4.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">

    <com.example.ajay.scrollabletoolbar.MyWebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"/>
</android.support.v4.widget.NestedScrollView>

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbarlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true">

    <android.support.v7.widget.Toolbar
        android:id="@+id/restricted_browser_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="@color/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FFFFFF"
            android:descendantFocusability="beforeDescendants"
            android:focusableInTouchMode="true">

            <EditText
                android:id="@+id/current_url"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerVertical="true"
                android:backgroundTint="@android:color/darker_gray"
                android:dropDownAnchor="@+id/restricted_browser_toolbar"
                android:hint="hint hint"
                android:imeOptions="actionGo|flagNoExtractUi"
                android:inputType="textNoSuggestions|textUri"
                android:paddingBottom="8dp"
                android:paddingEnd="8dp"
                android:paddingStart="8dp"
                android:singleLine="true"/>

            <ImageView
                android:id="@+id/clear_url_text"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_alignRight="@+id/current_url"
                android:layout_centerVertical="true"
                android:layout_marginRight="8dp"
                android:src="@android:drawable/ic_menu_close_clear_cancel"
                android:visibility="gone"/>
        </RelativeLayout>
    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

推荐答案

在一句话中,您只是不能WebView放在NestedScrollView内.这按预期工作.

In one sentence, you just can't put your WebView inside NestedScrollView. This is working as intended.

如果将WebView放在NestedScrollView(或ScrollView)内,则WebView使用

If you put a WebView inside NestedScrollView (or ScrollView), your WebView measures its dimensions with View.MeasureSpec.UNSPECIFIED requirement. No matter you set MATCH_PARENT or even fixed size like 100dp for your WebView's height. NestedScrollView forces its children measure themselves with View.MeasureSpec.UNSPECIFIED requirement. It all happens at NestedScrollView's measureChild() method. It goes like below :

@Override
protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
    ViewGroup.LayoutParams lp = child.getLayoutParams();
    int childWidthMeasureSpec;
    int childHeightMeasureSpec;
    childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, getPaddingLeft()
            + getPaddingRight(), lp.width);
    childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}

MeasureSpec.UNSPECIFIED的含义是,顾名思义,它可以是它想要的任何大小.我认为此幻灯片来自写作Google I/O 2013上的"Android自定义视图"会话将帮助您理解它.

And MeasureSpec.UNSPECIFIED means, as its label implies, it can be whatever size it wants. I think this slide from the "Writing Custom Views for Android" session at Google I/O 2013 will help you to understand it.

实际上,这是此主题,并根据这位Google工程师

Actually this is an issue which was discussed in this thread, and according to this Google engineer,

就像在桌面上一样,您将浏览器窗口的大小调整为页面的高度,以使其无法垂直滚动.滚动是在NestedScrollView中进行的,而不是在Webview中进行的.因此,页面中没有js的结果是看不到任何滚动事件,因此不知道您已经滚动到页面的末尾加载更多内容.

This is as if in desktop, you resize the browser window to height of the page so it can't scroll vertically. The scrolling is happening in NestedScrollView rather than webview itself. So a consequence of no js in the page sees any scroll events, so it doesn't know you've scrolled to the end of the page load more content.

因此,最重要的是,请勿WebView放在NestedScrollView内.您应该让WebView滚动网页本身.祝您编码愉快! :)

So, bottom line is, DO NOT put your WebView inside NestedScrollView. You should let WebView scroll the web page itself. Happy coding! :)

这篇关于webview的高度在nestedScrollView内无限增长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆