Android Webview底部有额外的空白区域

时间:2016-05-30 09:12:36

标签: android html android-webview android-xml android-drawable

我使用HTML的以下方法将本地图像加载到webview

   void loadingImage(WebView mWebView){
        mWebView.getSettings().setSupportZoom(false);
        mWebView.getSettings().setDisplayZoomControls(false);
        String mHead="<html><head><style type='text/css'>body{margin:auto auto;text-align:center;} img{width:100%25;} </style></head>";
        String mBody = "<body> <img src='my_image.jpg'/> </body></html>";
        mWebView.loadDataWithBaseURL("file:///android_res/drawable/", mHead + mBody, "text/html", "UTF-8", null);
        mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
   }

和XML部分

  <WebView
        android:id="@+id/my_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>     

但是使用这种方法,如果手机的方向是纵向的,我可以在向下滚动时在底部获得额外的空白区域。

图像的宽度比它的高度大1.5倍,因此我理解WebView加载图像并通过增加高度使内容为正方形(宽度和高度相等)但是通过在高度上添加额外的空白来增加它。

但是,如果手机的方向是横向的,它可以毫无问题地工作,而不会增加任何空白区域。

所以请帮助我了解问题的实际原因以及解决方法。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

试试这个。

 <WebView
        android:id="@+id/web_view"
        android:scrollbarStyle = "insideOverlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

答案 1 :(得分:0)

制作WebView身高wrap_content

<WebView
    android:id="@+id/web_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

即使在肖像模式下它也适用于我:)