webview不会在android中自动滚动

时间:2014-01-23 06:31:15

标签: android webview

在我的webview应用程序中,其中一个网页仅包含位于中心位置的元素。为了使网页与移动显示兼容,我需要中心部分通过缩放来填充显示。我使用了setInitialScale()和scrollTo()功能。但我没有得到我需要的东西。请帮帮我。缩放工作正常。但它不会滚动到中间部分。它仍然位于左上角,只是空白区域。

AdminActivity.java

package com.example.admin;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@SuppressWarnings("deprecation")
public class AdminActivity extends Activity {

private WebView webView;

@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        webView = (WebView)findViewById(R.id.webView4);
        webView.requestFocusFromTouch();

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setUseWideViewPort(true);                                                         
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setBuiltInZoomControls(true);

        webView.loadUrl("http://app.samworkshops.org/PasswordProtect.aspx");

        webView.setWebViewClient(new WebViewClient() {


            @Override
            public void onPageFinished(WebView view, String url) {


                webView.scrollTo(100,100);
                webView.setInitialScale(100);
            }


        });
    }


}

activity_admin.xml

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView4"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:windowSoftInputMode="adjustResize"
/>

2 个答案:

答案 0 :(得分:1)

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

就是这样!

答案 1 :(得分:0)

package com.example.admin;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@SuppressWarnings("deprecation")
public class AdminActivity extends Activity {

private WebView webView;

@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        webView = (WebView)findViewById(R.id.webView4);
   // for scrollabale webview
        webView.setVerticalScrollBarEnabled(true);
        webView.setHorizontalScrollBarEnabled(true);
        webView.requestFocusFromTouch();

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setUseWideViewPort(true);                                                         
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setBuiltInZoomControls(true);

        webView.loadUrl("http://app.samworkshops.org/PasswordProtect.aspx");

        webView.setWebViewClient(new WebViewClient() {


            @Override
            public void onPageFinished(WebView view, String url) {


                webView.scrollTo(100,100);
                webView.setInitialScale(100);
            }


        });
    }