禁用 webview 中的滚动?
到目前为止,我只是一名 iPhone 开发人员,现在我决定尝试一下 Android。我在 Android 上无法弄清楚如何以编程方式防止在 WebView
中滚动?
类似于 iPhone 阻止 onTouchMove
事件的功能会很棒!
Until now I have been an iPhone developer only and now I have decided to give Android a whirl. Something I haven't been able to figure out on Android is how to programmatically prevent scrolling in a WebView
?
Something similar to iPhones prevention of the onTouchMove
event would be great!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
这是我在 webview 中禁用所有滚动的代码:
仅隐藏滚动条,但不禁用滚动:
或者您可以尝试使用单列布局,但这仅适用于简单的情况页面并禁用水平滚动:
您还可以尝试使用垂直滚动滚动视图包装您的 webview 并禁用 webview 上的所有滚动:
并设置
不要忘记添加
webview.setOnTouchListener( ...)
上面的代码禁用 web 视图中的所有滚动。垂直 ScrollView 将允许滚动 WebView 的内容。Here is my code for disabling all scrolling in webview:
To only hide the scrollbars, but not disable scrolling:
or you can try using single column layout but this only works with simple pages and it disables horizontal scrolling:
You can also try to wrap your webview with vertically scrolling scrollview and disable all scrolling on the webview:
And set
Don't forget to add the
webview.setOnTouchListener(...)
code above to disable all scrolling in the webview. The vertical ScrollView will allow for scrolling of the WebView's content.让
WebView
忽略运动事件是错误的方法。如果WebView
需要了解这些事件怎么办?相反,子类化
WebView
并覆盖非私有滚动方法。如果您查看
WebView
的源 你可以看到onTouchEvent
调用doDrag
调用 overScrollBy。Making the
WebView
ignore motion events is the wrong way to go about it. What if theWebView
needs to hear about these events?Instead subclass
WebView
and override the non-private scroll methods.If you look at the source for
WebView
you can see thatonTouchEvent
callsdoDrag
which calls overScrollBy.我不知道你是否还需要它,但解决方案如下:
I don't know if you still need it or not, but here is the solution:
如果您的内容正确换行,则将边距详细信息添加到正文将阻止滚动,如下所示:
足够简单,并且代码和错误开销要少得多:)
Adding the margin details to the body will prevent scrolling if your content properly wraps, as so:
Easy enough, and a lot less code + bug overhead :)
在您的 WebView 上设置监听器:
Set a listener on your WebView:
我还没有尝试过这个,因为我还没有遇到这个问题,但也许你可以重写 onScroll 函数?
I haven't tried this as I have yet to encounter this problem, but perhaps you could overrive the onScroll function?
我的肮脏但易于实现且运行良好的解决方案:
只需将 webview 放入滚动视图中即可。使 webview 远大于可能的内容(在一维或两个维度,具体取决于要求)。 ..并根据需要设置滚动视图的滚动条。
禁用 Web 视图上的水平滚动条的示例:
我希望这有帮助;)
My dirty, but easy-to-implement and well working solution:
Simply put the webview inside a scrollview. Make the webview to be far too bigger than the possible content (in one or both dimensions, depending on the requirements). ..and set up the scrollview's scrollbar(s) as you wish.
Example to disable the horizontal scrollbar on a webview:
I hope this helps ;)
要禁用滚动,请使用此
To Disable scroll use this
我通过以下方式解决了闪烁和自动滚动问题:
但是,如果由于某种原因它仍然不起作用,只需创建一个扩展 WebView 的类,并在其上添加此方法:
我建议扩展 WebView,以提供更有效的控制,如禁用/启用滑动、启用/禁用触摸、侦听器、控制转换、动画、内部定义设置等。
I resolved the flicker and auto-scroll by:
However if it still does not work for some reason, simply make a class that extends WebView, and put this method on it:
I am suggesting to extend WebView, in order to provide more effective control, like disable/enable swipes, enable/disable touches, listeners, control transitions, animations, define settings internally so on..
这可能不是你问题的根源,但我花了几个小时试图找出为什么我的应用程序在 iPhone 上运行良好,导致 Android 浏览器不断抛出垂直/水平滚动条并实际移动页面。我有一个 html body 标签,高度和宽度设置为 100%,并且主体在不同位置充满了各种标签。无论我尝试什么,Android 浏览器都会显示滚动条并稍微移动页面,特别是在快速滑动时。对我来说无需在 APK 中执行任何操作的解决方案是将以下内容添加到 body 标签:
似乎 body 标签的默认边距已应用于 droid 但在 iphone 上被忽略
This may not be the source of your problem but I have spent hours trying to track down why my application that worked fine on the iphone cause the android browser to constantly throw up the vertical/horizontal scrollbars and actually move the page. I had an html body tag with height and width set to 100%, and the body was full of various tags in different locations. No matter what I tried, the android browsers would show their scroll bars and move the page just a little bit, particularly when doing a fast swipe. The solution that worked for me without having to do anything in an APK was to add the following to the body tag:
It seems the default margins for body tag were being applied on the droid but ignored on the iphone
如果您子类化 Webview,则只需重写 onTouchEvent 即可过滤掉触发滚动的移动事件。
If you subclass Webview, you can simply override onTouchEvent to filter out the move-events that trigger scrolling.
研究上面的答案几乎对我有用......但我仍然有一个问题,我可以“抛出”2.1 上的视图(似乎已在 2.2 和 2.3 中修复)。
这是我的最终解决方案
studying the above answers almost worked for me ... but I still had a problem that I could 'fling' the view on 2.1 (seemed to be fixed with 2.2 & 2.3).
here is my final solution
这应该是完整的答案。正如@GDanger 建议的那样。
扩展 WebView 以覆盖滚动方法并将自定义 WebView 嵌入到布局 xml 中。
然后嵌入布局文件,如下所示。
然后在活动中,也使用一些附加方法来禁用滚动条。
This should be the complete answer. As suggested by @GDanger .
Extend WebView to override the scroll methods and embed the custom webview within layout xml.
And then embed in layout file as follows
Then in the Activity, use some additional methods for disabling scrollbars too.