2013-04-09 27 views
8

您好我有一个网页视图,我希望它适合屏幕,使得用户不必之前滚动水平或垂直的Android的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; 
+0

检查此链接:http://stackoverflow.com/a/9756084/1168654和http://stackoverflow.com/questions/11922861/how-to-justify-text-on-a-textview- made-easy-android/11922862#11922862和http://stackoverflow.com/questions/15894644/how-we-get-the-text-of-a-texview-to-be-justified-dynamically/15894704#15894704 – 2013-04-09 10:33:35

回答

1

为了使web视图扩展到整个屏幕,请将webview添加到您的活动布局xml中,并确保您设置了layout_widthlayout_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> 
5

使用以下方法。

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