在WebView Android中删除不需要的空格

时间:2012-01-01 16:39:20

标签: android image android-webview padding space

我已经开始使用WebView开发应用程序了。实际上我正在使用Webview加载图像(我喜欢使用该类的内置缩放控件)。我可以成功加载图像,但我可以看到一些恼人的白色空间。我找不到删除它的方法。我的图片尺寸为750 * 1000。我在下面附上了我的代码。

webview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <WebView
        android:id="@+id/webView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

ImageViewer.java

package com.android.test;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class ImageViewer extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);

        String loadUrl = "file:///android_res/drawable/splash_001.jpg";

        WebView image = (WebView) findViewById(R.id.webView);
        image.clearView();
        image.clearCache(true);
        image.getSettings().setBuiltInZoomControls(true);
        image.getSettings().setSupportZoom(true);
        image.getSettings().setLoadWithOverviewMode(true);
        image.getSettings().setUseWideViewPort(true);
        image.loadUrl(loadUrl);
    }

}

12 个答案:

答案 0 :(得分:52)

默认情况下, webview 在正文中有一些边距/填充。如果要删除该填充/边距,则覆盖正文标记并添加边距,如:

<body style="margin: 0; padding: 0">

答案 1 :(得分:21)

如果您正在谈论的空白区域位于右侧,则可以将其添加到网页浏览中。

mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

这应该使白色空间消失,只要它位于滚动条的右侧。

答案 2 :(得分:17)

默认情况下,HTML网页的填充和边距为10px;您必须在头部或css文件中设置:

<style type="text/css">
html, body {
width:100%;
height: 100%;
margin: 0px;
padding: 0px;
}
</style>

这对我有用。

答案 3 :(得分:8)

我的WebView中呈现的HTML内容边缘有刺激性的空白区域(尽管该内容中没有图像)。起初我认为它与上面提到的CSS问题有关。但是通过使用样式表来改变HTML,BODY等的背景,它看起来越来越像在WebView周围填充。

user3241873在上面显示了如何调整WebView本身的填充,但我发现当Eclipse创建的默认Android应用程序生成布局文件时(RelativeLayout&#34; container&#34;而不是&#34; LinearLayout& #34;),它将这些属性添加到R​​elativeLayout:

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

如果在res / values / dimens.xml中查找这些填充值,您会发现它们已设置为16dp。当我改为0dp时,它删除了难看的空白区域:)

如果有人用WebView替换了默认的(Hello World)TextView,那么父亲RelativeLayout的填充就像家里一​​个讨厌的亲戚一样。

答案 4 :(得分:3)

使用WebView的setScrollBarStyle方法,并将参数提供为View.SCROLLBARS_INSIDE_OVERLAY

答案 5 :(得分:1)

这是一个可以测试使用html模板的示例,然后您可以将“body”文本替换为您想要的任何内容...在您的情况下,为图像的html。您可以根据需要使用css来设置html的样式。希望这有助于您了解更多如何使用本地资产。如果您需要更多帮助,请发表评论。

一个不容错过的关键路线是<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,target-densityDpi=device-dpi" />

将此作为模板使用:(assets / template.html)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,target-densityDpi=device-dpi" />
    <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
[CONTENT]
</body>
</html>

使用这样的css:(assets / style.css)

@font-face {
  font-family: Roboto;
  src: url(file:///android_asset/fonts/Roboto.ttf);
}

html, div {
    margin: 0;
    padding: 0;
}

body {
    font-family: Roboto;
    font-size: 1.0em;
    color: rgb(61,61,61);
    text-align: left;
    position:relative;
    padding: 40px;
    background-color: #eeeeee;
}

然后在您的WebView类中:(您的扩展WebView的类)

public void setText(CharSequence text, int color){

       try {

           InputStream is = getResources().getAssets().open("article_view.html");
           int size = is.available();
           byte[] buffer = new byte[size];
           is.read(buffer);
           is.close();

           loadDataWithBaseURL("file:///android_asset/",
               new String(buffer).replace("[CONTENT]", text.toString()),
               "text/html", "UTF-8", null);

       } catch (IOException e) {
           e.printStackTrace();
       }

}

答案 6 :(得分:1)

我有[这是我的 activity.xml ]

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

我刚刚删除了relativeLayout中的填充

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" 
tools:context=".MainActivity">

我尝试了上述所有解决方案,但他们并没有为我做得很好。 这个对我有用。

答案 7 :(得分:0)

我真的不知道你指的是哪个空格(也许图像有填充等),但通常如果你在WebView中有任何布局问题,你会尝试通过提供适当的(内联)css来修复它们-stylesheet。

答案 8 :(得分:0)

将webview.xml中的填充设置为0dp

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<WebView
    android:id="@+id/webView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp" />

答案 9 :(得分:0)

Kotlin 答案

例如,我以这种方式删除了 iFrame 和 WebView 之间的空间:

val url = "www.stackoverflow.com"

val iframeExample = "<html><body style=\"margin: 0; padding: 0\"><iframe width=\"100%\" src=\"$url\" frameborder=\"0\" allowfullscreen></iframe></body></html>"

webView.loadData(iframeExample, "text/html", "utf-8")

答案 10 :(得分:-1)

我已经知道了。

这里是我的逻辑代码,当应用程序打开网站时,您必须获得webview的大小,然后将其设置为高度

这里是我的代码

 ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) webpage.getLayoutParams();
        p.height = webpage.getHeight();
        Log.e(" webpage.getHeight()", String.valueOf(webpage.getHeight()));
        webpage.setLayoutParams(p);

希望你能把我的代码和我的答案:)用于任何设备甚至标签:)

答案 11 :(得分:-1)

我的情况,我的webview顶部有大填充。所以,设置scrollbarSize =“0dp”。在AS 2.2.3中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"
android:scrollbarSize="0dp"
/>
相关问题