9

嗨,我有一个 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);

只是一些注释:

要缩放 web 视图:

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

2 回答 2

5

使用以下方法。

webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setLoadWithOverviewMode(true);
于 2015-06-05T08:51:13.577 回答
2

要使 webview 扩展到整个屏幕,请在活动布局 xml 中添加 webview,并确保将layout_widthand设置layout_heightfill_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>
于 2013-05-03T05:49:23.417 回答