Java Android WebView 不可滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20910405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:15:37  来源:igfitidea点击:

Android WebView isn't scrollable

javaandroidwebviewscroll

提问by Abascus

My Android WebViewisn't scrollable.

我的安卓WebView不能滚动。

XML Code:

XML 代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#87cefa"
android:gravity="left"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<WebView
    android:id="@+id/webView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerHorizontal="true" />

I'm loading the Website with the normal webview.loadUrl(url);function and sometimes with the webview.loadDataWithBaseURL("", htmlContent, "text/html", "UTF-8", "");function, both of them display the page, but they are not scrollable

我正在使用正常webview.loadUrl(url);功能加载网站,有时使用该webview.loadDataWithBaseURL("", htmlContent, "text/html", "UTF-8", "");功能加载网站,它们都显示页面,但它们不可滚动

Init:

在里面:

setContentView(R.layout.activity_main); webview = (WebView)this.findViewById(R.id.webView); webview.setVerticalScrollBarEnabled(true); webview.setHorizontalScrollBarEnabled(true);

setContentView(R.layout.activity_main); webview = (WebView)this.findViewById(R.id.webView); webview.setVerticalScrollBarEnabled(true); webview.setHorizontalScrollBarEnabled(true);

Loading:

加载:

            webview.loadUrl(url);
            [either one of them or the other]
    webview.loadDataWithBaseURL("", htmlContent, "text/html", "UTF-8", "");

回答by Nathaniel D. Waggoner

I've posted this as a comment as I think this may be a duplicate, but I would look at this question:

我已将此作为评论发布,因为我认为这可能是重复的,但我会看看这个问题:

Android WebView Scrollable

Android WebView 可滚动

Which provides a few calls which I believe will solve this issue.

它提供了一些我相信可以解决这个问题的电话。

Another issue:

另一个问题:

Try changing your webview from "Wrap Content" to some specific height. Then set the scrollables.

尝试将您的 webview 从“Wrap Content”更改为某个特定高度。然后设置滚动条。

Try something like 300dp and go from there.

尝试像 300dp 这样的东西,然后从那里开始。

回答by kabuko

Don't use wrap_contentfor your height. If your WebViewhas the same height as its content, then there's never anything to scroll as obviously the content all fits by definition (though given you're loading content dynamically this may not be exactlywhat's happening). Try setting the height to match_parentor a fixed value.

不要wrap_content用于你的身高。如果您WebView的高度与其内容相同,则永远不会有任何内容可以滚动,因为显然内容都符合定义(尽管鉴于您正在动态加载内容,这可能并不完全正确)。尝试将高度设置为match_parent或 固定值。

回答by Sreedhar GS

I have found a solution for this, All you need is to put WebView definition in "

我已经找到了解决方案,您只需要将 WebView 定义放在“

layout/content_my.xml

布局/content_my.xml

" not in "activity_my.xml"

" 不在 "activity_my.xml" 中

<WebView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/webView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

And inside the MyActivity.java

在 MyActivity.java 里面

    WebView webview = (WebView)findViewById(R.id.webView);
    webview.loadUrl("file:///android_asset/index.html");
    webview.setVerticalScrollBarEnabled(true);
    webview.setHorizontalScrollBarEnabled(true);

回答by Matthias Huber

I had some other elements in my relative layout, which I switched to invisible. This made my webview not scrollable. My solution was:

我的相对布局中有一些其他元素,我将其切换为不可见。这使我的 webview 不可滚动。我的解决方案是:

relativelayout.removeallchilds();
relativelayout.addchild(webview);
relativelayout.addchild(overlaybutton);

回答by Abhishek Choudhary Khatkar

Hey I had the Same Problem with webview. It was not scrolling even with the match_parent attribute and very simple code.

嘿,我在 webview 上遇到了同样的问题。即使使用 match_parent 属性和非常简单的代码,它也不会滚动。

The problem is not in your xml file. Its in your Java file. In java file of that activity Override onPause AND onResume methods and iclude this code

问题不在您的 xml 文件中。它在您的 Java 文件中。在该活动的 java 文件中覆盖 onPause 和 onResume 方法并包含此代码

@Override
protected void onResume() {
    super.onResume();
    browser.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    browser.onPause();
}

It is very important that you Pause your WebView AND then Resume it again. Hope that will solve your problem.

暂停 WebView 然后再次恢复它非常重要。希望能解决你的问题。

回答by Yashaswi Bharadwaj

add this line and it will start working

添加这一行,它将开始工作

webview.getSettings().setJavaScriptEnabled(true);

Hope, this solves your problem?

希望,这能解决您的问题吗?