Android webview适合所有内容到屏幕

时间:2013-04-09 10:26:13

标签: android android-webview android-windowmanager

嗨我有一个webview,我希望它适合屏幕,这样用户就不必水平或垂直滚动​​

我之前搜索过,很多人建议如下,但它只限制宽度,用户仍然需要垂直滚动:

engine = (WebView) (findViewById(R.id.webView1));
engine.loadUrl(www.example.com); 
engine.getSettings().setLoadWithOverviewMode(true);
engine.getSettings().setUseWideViewPort(true);

我尝试了另一种方法,使用缩放方法,但它不起作用.....我得到窗口宽度(1024像素),网页内容宽度也是1024像素!因此缩放不起作用....有没有任何方法可以获得正确的比例?

或....还有其他方法吗?....谢谢!!

// get the window width and height
    Point outSize = new Point();
    getWindowManager().getDefaultDisplay().getSize(outSize);
    width = outSize.x;
    height = outSize.y;
    Log.i("Menuboard", "width: " + Integer.toString(width) + ", height: "
            + Integer.toString(height));

WebViewClient client = new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
            int x = engine.getWidth();
            int y = engine.getHeight();

            Log.i("Menuboard", "web width: " + Integer.toString(x)
                    + ", height: " + Integer.toString(y));

            int scale1 = width * 100/x;
            int scale2 = height * 100/y;

            Log.i("Menuboard", "Scale1: " + Integer.toString(scale1)
                    + ", Scale2: " + Integer.toString(scale2));

            if (scale1 < scale2) {
                engine.setInitialScale(scale1);
                Log.i("Menuboard", "scale1");
            } else if (scale1 > scale2) {
                engine.setInitialScale(scale2);
                Log.i("Menuboard", "scale2: " + Integer.toString(scale2));
            }
        }
    };

    engine = (WebView) (findViewById(R.id.webView1));
    engine.loadUrl(string);
    engine.getSettings().setBuiltInZoomControls(true);
    engine.setPadding(0, 0, 0, 0);
            engine.setWebViewClient(client);

只是一些注释:

缩放网页视图:

engine = (WebView) (findViewById(R.id.webView1));
engine.loadUrl(www.example.com);
engine.getSettings().setBuiltInZoomControls(true);
engine.setPadding(0, 0, 0, 0);
engine.setInitialScale(50); // 0 - 100 where 100 is full scale

获取窗口宽度和高度:

Point outSize = new Point();
getWindowManager().getDefaultDisplay().getSize(outSize);
width = outSize.x;
height = outSize.y;

2 个答案:

答案 0 :(得分:5)

使用以下方法。

webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setLoadWithOverviewMode(true);

答案 1 :(得分:2)

要将webview展开到整个屏幕,请在活动布局xml中添加webview,并确保将layout_widthlayout_height设置为fill_parent。这是一个简单的例子:

  <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/webview"
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"/>
  </LinearLayout>