启用放大WebView android

时间:2015-04-02 13:23:37

标签: java android webview

我无法在Android中的WebView上启用缩放钳

我是编程新手,已经尝试了所有东西而无法解决问题。

在这里新建,我无法附上图片。

WebViewActivity.java

package com.mkyong.android;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class WebViewActivity extends Activity {

    private WebView webView;

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

        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setBuiltInZoomControls(true);
        webSettings.setSupportZoom(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://200.99.150.163/sacsptrans/hrecinicial.aspx");


        webView.setWebViewClient(new WebViewClient()
        {
                        // prevent open in external browser
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                return false;
            }   

        });

    }

MainActivity.java

package com.mkyong.android;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    private Button button;

    public void onCreate(Bundle savedInstanceState) {
        final Context context = this;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button) findViewById(R.id.buttonUrl);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(context, WebViewActivity.class);
                startActivity(intent);
            }

        });

    }

}

4 个答案:

答案 0 :(得分:3)

webView.getSettings().setBuiltInZoomControls(true); 

如果您需要更多提前webView,可以使用webview sample

答案 1 :(得分:2)

要在webView上启用缩放功能,请在 onTuchEvent覆盖方法中添加以下代码:

webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setSupportZoom(true);

如果您的webview转到另一个网址,并且您希望保留缩放级别 使用webSettings

webview.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);

如果您想在webview加载时使用预定义的缩放:

mWebView.setInitialScale(ZOOM_LEVEL);  // int value 50 = 50% -- 200 = 200%

它适用于所有设备分辨率。

更新:如果其中任何一个正在运行(weeeird ......)请尝试以下操作:

public class Main extends Activity {
  private WebView myWebView;
  private static final FrameLayout.LayoutParams ZOOM_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,Gravity.BOTTOM);

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.webview);
    this.myWebView = (WebView) this.findViewById(R.id.webView);

    FrameLayout mContentView = (FrameLayout) getWindow().
    getDecorView().findViewById(android.R.id.content);
    final View zoom = this.myWebView.getZoomControls();
    mContentView.addView(zoom, ZOOM_PARAMS);
    zoom.setVisibility(View.GONE);

    this.myWebView.loadUrl("http://www.facebook.com");
  }
}

来自this question.

答案 2 :(得分:2)

webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);

答案 3 :(得分:0)

mWebView = (WebView) findViewById(R.id.webView);

WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setBuiltInZoomControls(true);

mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDisplayZoomControls(true);

mWebView.setInitialScale(400); // zoom x4 (400%)
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);