如何禁用 webview 的垂直滚动以将整个页面加载为不同的列

发布于 2024-10-31 21:38:22 字数 341 浏览 4 评论 0原文

我想禁用 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>";

感谢您抽出时间。

I want to disable webview vertical scroll view.Load the web page as columns in webview
by using this way I can give the user a book reading effect.

this is the style I added in html.it is not working at all.

  loadingStringHorizontal="<style type='text/css'>body { width:200px;height:400px;
    -webkit-column-gap:17px;-webkit-column-width:170px;
     text-align:justify ;} </style>";

Thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

○闲身 2024-11-07 21:38:22

您可以尝试单列布局

myWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

,或者在 XML 中,您可以在滚动条中不放置任何内容,

<WebView
    android:scrollbars="none" />

然后设置 myWebView.setScrollContainer(false);

You can try single column layout

myWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

or in XML you can put none in scrollbar

<WebView
    android:scrollbars="none" />

then set myWebView.setScrollContainer(false);

夏日落 2024-11-07 21:38:22

首先,您必须添加自定义 webview 以禁用垂直向下滚动,

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);

当你的页面完全加载到 webview 上后,你必须在你的 webview 上运行 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()");
    }

first you have to add custom webview for disable vertical scroll down

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);
    }
}

then you can create multi columns webview with javascript
(https://stackoverflow.com/a/9165698/1140304)

You have to apply these lines to the setting of 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);

after your page completely loaded on webview , you have to run javascript code on your webview

 webView_.setWebViewClient(new WebViewClient(){

            public void onPageFinished(WebView view, String url) {
                injectJavascript();
            }

        });

this is javascript code

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()");
    }
对风讲故事 2024-11-07 21:38:22

这是一个简单但有效的解决方案,唯一适合我(参考此处):

body{
    position:fixed;
    top:0;
}

Here a simple but effective solution, the only working for me (reference here):

body{
    position:fixed;
    top:0;
}
可爱咩 2024-11-07 21:38:22

尝试添加 CSS overflow: auto;overflow-y: hide;

Try adding CSS overflow: auto;overflow-y: hidden;

我家小可爱 2024-11-07 21:38:22

带有滚动开关的自定义 WebView

class LockableWebView : WebView {
    private var scrollable = true
    constructor(context: Context) : super(context)
    constructor(context: Context, @Nullable attrs: AttributeSet?) : super(context, attrs)
    constructor(
        context: Context,
        @Nullable attrs: AttributeSet?,
        defStyleAttr: Int
    ) : super(context, attrs, defStyleAttr)
    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(ev: MotionEvent): Boolean = scrollable && super.onTouchEvent(ev)
    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean = scrollable && super.onInterceptTouchEvent(ev)
    fun setScrollingEnabled(enabled: Boolean) { scrollable = enabled }
    fun onScrollStateChanged(startDelay: Long = 100, stopDelay: Long = 400, listener: (Boolean) -> Unit) {
        setOnTouchListener { v, event ->
            when (event.action) {
                MotionEvent.ACTION_SCROLL, MotionEvent.ACTION_MOVE -> handler.postDelayed({ listener.invoke(true) }, startDelay)
                MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_UP -> handler.postDelayed({ listener.invoke(false) }, stopDelay)
            }
            v.performClick()
            false
        }
    }
}



webView.setScrollingEnabled(true/false)

Custom WebView with on/off toggle for scrolling

class LockableWebView : WebView {
    private var scrollable = true
    constructor(context: Context) : super(context)
    constructor(context: Context, @Nullable attrs: AttributeSet?) : super(context, attrs)
    constructor(
        context: Context,
        @Nullable attrs: AttributeSet?,
        defStyleAttr: Int
    ) : super(context, attrs, defStyleAttr)
    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(ev: MotionEvent): Boolean = scrollable && super.onTouchEvent(ev)
    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean = scrollable && super.onInterceptTouchEvent(ev)
    fun setScrollingEnabled(enabled: Boolean) { scrollable = enabled }
    fun onScrollStateChanged(startDelay: Long = 100, stopDelay: Long = 400, listener: (Boolean) -> Unit) {
        setOnTouchListener { v, event ->
            when (event.action) {
                MotionEvent.ACTION_SCROLL, MotionEvent.ACTION_MOVE -> handler.postDelayed({ listener.invoke(true) }, startDelay)
                MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_UP -> handler.postDelayed({ listener.invoke(false) }, stopDelay)
            }
            v.performClick()
            false
        }
    }
}



webView.setScrollingEnabled(true/false)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文