Android: sample to show WebView with scrolling

2019-01-26 02:00发布

问题:

This should be pretty basic, I just can't find out how. I have a webView that shows a page to is too long to show all at once. Right now, it shows the scroll bar, but it doesn't really scroll. It just kinda wiggles a little bit just enough to let you know it's an active scroll bar.

Hopefully, in the xml layout, as much as possible, I would like to have the page scroll as needed to show the whole page.

my xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
    >
    <WebView
        android:id="@+id/news_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
</LinearLayout>

onCreate call:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.news);

    WebView webView = (WebView) findViewById(R.id.news_view);
    webView.setVerticalScrollBarEnabled(true);

    webView.loadUrl("http://www.example.com/index.html");
}

回答1:

did you think about using ScrollView ??

<ScrollView android:layout_width="fill_parent" 
            android:layout_height="86px" 
            android:id="@+id/scrollTxtDescription"
            android:layout_below="@id/txtLieuPromo1" 
            android:layout_alignLeft="@id/txtLieuPromo1">

            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/layoutTxtDescription"
                >

                <WebView android:id="@+id/txtDescription" 
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    />
            </LinearLayout>
        </ScrollView>


回答2:

i use the next code and it worked for me (tested on android 2.3 and android 4.2 )

layout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <WebView
            android:id="@+id/web_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>

</ScrollView>

code:

final WebSettings settings = mWebView.getSettings();
settings.setDomStorageEnabled(true);
settings.setJavaScriptEnabled(true);
mWebView.setVerticalScrollBarEnabled(true);

mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
        super.shouldOverrideUrlLoading(view, url);
        return false;
    }

    @Override
    public void onPageFinished(final WebView view, final String url) {
        super.onPageFinished(view, url);
        mWebView.requestLayout();
    }

});
mWebView.loadUrl(uri);