在 webview 中禁用滚动? [英] Disable scrolling in webview?
问题描述
到目前为止,我只是一名 iPhone 开发人员,现在我决定试一试 Android.我在 Android 上无法弄清楚的是如何以编程方式防止在 WebView
中滚动?
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
?
类似于 iPhone 防止 onTouchMove
事件的东西会很棒!
Something similar to iPhones prevention of the onTouchMove
event would be great!
推荐答案
这是我在 webview 中禁用所有滚动的代码:
Here is my code for disabling all scrolling in webview:
// disable scroll on touch
webview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
<小时>
只隐藏滚动条,但不禁用滚动:
To only hide the scrollbars, but not disable scrolling:
WebView.setVerticalScrollBarEnabled(false);
WebView.setHorizontalScrollBarEnabled(false);
<小时>
或者您可以尝试使用单列布局,但这仅适用于简单页面,并且会禁用水平滚动:
or you can try using single column layout but this only works with simple pages and it disables horizontal scrolling:
//Only disabled the horizontal scrolling:
webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
<小时>
您还可以尝试使用垂直滚动滚动视图包裹您的网络视图并禁用网络视图上的所有滚动:
You can also try to wrap your webview with vertically scrolling scrollview and disable all scrolling on the webview:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical" >
<WebView
android:id="@+id/mywebview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none" />
</ScrollView>
并设置
webview.setScrollContainer(false);
不要忘记添加上面的 webview.setOnTouchListener(...)
代码以禁用 webview 中的所有滚动.垂直 ScrollView 将允许滚动 WebView 的内容.
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 中禁用滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!