【问题标题】:Android Webview has extra white space at the bottomAndroid Webview 底部有额外的空白
【发布时间】:2016-05-30 09:12:35
【问题描述】:

我正在使用下面的 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 加载图像并通过增加高度使内容为正方形(宽度和高度相等),但仅通过添加额外的空白来增加它高度。

但如果手机的方向是横向的,它可以正常工作,不会添加任何空白。

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

【问题讨论】:

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


    【解决方案1】:

    让你的WebView 高度wrap_content

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

    即使在纵向模式下也适用于我 :)

    【讨论】:

    • 不,这不是关于视图的高度,而是关于内容的高度。
    • 你是什么意思?在你的问题中放一些截图
    • 4 年后仍然有用
    【解决方案2】:

    试试这个。

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

    【讨论】:

    • 因为它不是关于滚动条样式,而是关于 WebView 的内容大小