Android WebView Scrollable

2019-01-19 18:18发布

问题:

I'm making a browser based on android WebView class. I want to enable both horizontal and vertical scrolls on the webview and want it to behave exactly like the android browser? Is there any setting for that in the manifest file or shall i override the default webviewclient class.

回答1:

The WebView documentation says:

By default, a WebView provides no browser-like widgets

It also says:

A WebView has several customization points where you can add your own behavior. These are:

...

Creating and setting a WebViewClient subclass. It will be called when things happen that impact the rendering of the content, eg, errors or form submissions. You can also intercept URL loading here.

Which suggests WebViewClient has nothing to do with the scroll bars, since they're not content.

I was going to suggest putting a WebView inside of a ScrollView, but looking at this link it seems WebView's default behaviour is to include scroll bars, which makes sense since a lot of scrollbar functionality is defined in the high level View class. Have you tried just making a regular WebView? If so, have you tried adding the following in your java code?

WebView v = (WebView) findViewById(R.id.webview); 
v.setVerticalScrollBarEnabled(true);
v.setHorizontalScrollBarEnabled(true);