我想禁用webview垂直滚动视图。将网页作为webview中的列添加 通过这种方式,我可以给用户一个书籍阅读效果。
这是我在html中添加的样式。它根本不起作用。
loadingStringHorizontal="<style type='text/css'>body { width:200px;height:400px;
-webkit-column-gap:17px;-webkit-column-width:170px;
text-align:justify ;} </style>";
感谢您的时间。
答案 0 :(得分:4)
您可以尝试单列布局
myWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
或在XML中,您可以在滚动条中添加任何内容
<WebView
android:scrollbars="none" />
然后设置myWebView.setScrollContainer(false);
答案 1 :(得分:2)
首先,您必须添加自定义网页视图以禁用垂直向下滚动
class CustomWebView2 extends WebView
{
Context mContext = null;
private float start_y = -1;
private String DEBUG_TAG = " touch:";
public CustomWebView2(Context context)
{
super(context);
mContext = this.getContext();
}
public CustomWebView2(Context context, AttributeSet attrs)
{
super(context, attrs);
mContext = context;
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
int action = event.getAction();
switch (action)
{
case (MotionEvent.ACTION_DOWN):
/**
* get Y position
*/
start_y = event.getY();
break;
case (MotionEvent.ACTION_MOVE):
/**
* if you scroll to up more than 60px
* webView scroll back to Y=0
*/
if(start_y-event.getY()>60)
{
scrollTo(getScrollX(),0);
return true;
}
break;
case (MotionEvent.ACTION_UP):
Log.d(DEBUG_TAG, "Action was UP");
break;
}
return super.onTouchEvent(event);
}
}
然后您可以使用javascript创建多列webview (https://stackoverflow.com/a/9165698/1140304)
您必须将这些行应用于webview的设置
CustomWebView webView_ = (CustomWebView) view.findViewById(R.id.webView);
webView_.getSettings().setUseWideViewPort(true);
webView_.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);
/**
* hide vertical scrollBar
*/
webView_.setVerticalScrollBarEnabled(false);
将您的网页完全加载到网页视图后,您必须在网络视图上运行javascript代码
webView_.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url) {
injectJavascript();
}
});
这是javascript代码
public void injectJavascript() {
String js = "javascript:function initialize() { " +
"var d = document.getElementsByTagName('body')[0];" +
"var ourH = window.innerHeight; " +
"var ourW = window.innerWidth; " +
"var fullH = d.offsetHeight; " +
"var pageCount = Math.floor(fullH/ourH)+1;" +
"var currentPage = 0; " +
"var newW = pageCount*ourW; " +
"d.style.height = ourH+'px';" +
"d.style.width = newW+'px';" +
"d.style.webkitColumnGap = '2px'; " +
"d.style.margin = 0; " +
"d.style.webkitColumnCount = pageCount;" +
"}";
webView_.loadUrl(js);
webView_.loadUrl("javascript:initialize()");
}
答案 2 :(得分:1)
这是一个简单但有效的解决方案,仅适用于我(参考here):
body{
position:fixed;
top:0;
}
答案 3 :(得分:0)
尝试添加CSS overflow: auto;overflow-y: hidden;