如何修复WebView以布局边缘

时间:2014-02-20 06:25:22

标签: android webview

我有一个使用webView的应用程序。它的属性是填充父级。我也全屏。但问题是,当我向下拖动它时,即使在结束之后,webView仍然可以向下或向上或向下推动,这使得它看起来很难看。我们可以将webView固定到边框并锁定,以便它不会超出其结束范围。请提前帮助,而不是。

修改

以下是我在WebView中打开IMDB网站的示例图片:

enter image description here

现在我们可以看到,webview /网站从KindleFire广告开始的地方开始。但当我向下拖动它时,我能够将webview移动到这个位置。虽然如果我离开它,它将再次回到顶部,但我想要的是它在结束后不会移动,这意味着Kindle Fire Ad应该始终位于屏幕的左上角,即使用户把手指拖到屏幕上。希望我能理解,请问我是不是。

这是我的Layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#074068"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/controls"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_margin="4dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/back"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:background="@drawable/back"
            android:layout_weight="1" />

        <Button
            android:id="@+id/reload"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:background="@drawable/reload"
            android:layout_weight="1" />

        <Button
            android:id="@+id/stop"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:background="@drawable/stop"
            android:layout_weight="1" />



        <Button
            android:id="@+id/front"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:background="@drawable/front"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="8"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="4dp"
        android:orientation="horizontal" >

        <WebView
            android:id="@+id/webView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

    </LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

尝试将图片监听器设置为webview并设置适当的属性。这将限制页面的滚动 实现PictureListener的示例代码,

webview.getSettings().setJavaScriptEnabled(true);
webview.setPictureListener(this);

@Override
@Deprecated
public void onNewPicture(android.webkit.WebView view, Picture picture) 
{
    webview.scrollTo(700, 600);     
}

你会通过尝试这个来获得一些想法,但这不是你想要的解决方案。 更改webview布局宽度&amp;高度与父母相匹配。

答案 1 :(得分:0)

我遇到了同样的问题,但通过设置webview选项解决了问题,如下面的

    mWebView.setVerticalFadingEdgeEnabled(false);
    //mWebView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
    mWebView.getSettings().setJavaScriptEnabled(true);
    //webSettings.setLoadWithOverviewMode(true);
    //webSettings.setUseWideViewPort(true);
    webSettings = mWebView.getSettings();
    //webSettings.setDefaultZoom(ZoomDensity.FAR);

    mWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setTextSize(TextSize.NORMAL);
    mWebView.getSettings().setPluginsEnabled(true);
    mWebView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Log.v("here","here");
            view.scrollBy(0, view.getContentHeight());
        }
    });

这在我的案例中非常完美。