【问题标题】:Android: sample to show WebView with scrollingAndroid:通过滚动显示 WebView 的示例
【发布时间】:2011-05-23 13:32:41
【问题描述】:

这应该是非常基本的,我只是不知道怎么做。我有一个 webView 显示页面太长而无法一次显示。现在,它显示滚动条,但并没有真正滚动。它只是有点摆动,足以让您知道它是一个活动的滚动条。

希望在 xml 布局中,我希望尽可能让页面滚动以显示整个页面。

我的 xml 布局文件:

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

        <WebView
            android:id="@+id/news_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>

onCreate 调用:

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

【问题讨论】:

  • “有点移动”是什么意思?请更清楚地说明什么不能正常工作。
  • 基本上,它会摆动一点,让您知道它是一个活动滚动条,但滚动长度是可见视图的长度。
  • 我试过了,看起来页面没有足够的信息让你注意到滚动。尝试加载更多内容的页面,例如 www.thinkgeek.com :)
  • 您使用的初始比例是多少?
  • @Donal:没有做任何关于缩放的事情,所以我想是默认值。

标签: android scroll webview


【解决方案1】:

你考虑过使用 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>

【讨论】:

  • 我尝试了几种不同的方法,但总是导致一些 xml 错误。这是什么sdk版本?
  • 我已经在 Android 1.6 中尝试过,它可以工作,我的 webView 在 ScrollView 中,它工作正常,请参阅我的编辑
【解决方案2】:

我使用下一个代码,它对我有用(在 android 2.3 和 android 4.2 上测试)

布局:

<?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>

代码:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    相关资源
    最近更新 更多