删除webView中的边距。

3

我有一个关于webView的问题。如您在图片中所见,有白色边缘。我想让这张图片填满我的屏幕。我该怎么做?

这是图片:

enter image description here

这是代码:

    WebView web = new WebView(getContext());
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setBuiltInZoomControls(true);

String imagePath = id;
String html = "<html><body><img src=\""+ imagePath + "\"></body></html>";
web.loadDataWithBaseURL("file:///mnt/sdcard/DinEgen/", html, "text/html","utf-8",null);
web.setPadding(0, 0, 0, 0);

web.setInitialScale(1);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setLoadWithOverviewMode(true);
web.getSettings().setUseWideViewPort(true);
web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
web.setScrollbarFadingEnabled(false);

XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:weightSum="100">

        <android.support.v4.view.ViewPager
        android:id="@+id/presentation_viewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:layout_weight="95"

        />
    <LinearLayout
        android:id="@+id/presentation_indicatorLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:layout_weight="5"
        android:gravity="center"
        android:background="@drawable/choose_small_normal"
        >
    </LinearLayout>
</LinearLayout>

你能发布你的XML文件吗? - AkashG
不要动态地将WebView添加到LinearLayout中,而是使用RelativeLayout,在其中添加WebView和ViewPager。将ViewPager的alignParentBottom设置为true,这样您就可以看到布局的外观,并且不会在两者之间留下空间。 - AkashG
2个回答

15

请使用以下标签替换您的标签:

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

这里有一个关于在 webview 中显示图片的提示:添加适应屏幕宽度的样式。对于各种屏幕尺寸都非常有效:

<style type='text/css'>
       img {max-width: 100%;height:initial;} div,p,span,a {max-width: 100%;}
       </style>

好的,问题可能在HTML中。我已经编辑了我的回答。如果有帮助,请告诉我。 - Ronen Yacobi
np。为您的图片添加另一个提示:max-width设置 - Ronen Yacobi
1
在这里使用<body>示例进行工作。谢谢! :) - Felipe Caldas
@RonenYacobi 你好,看起来你可以帮我。请查看这个链接:http://stackoverflow.com/questions/41449446/remove-blank-sapaces-in-webveiw-not-work - Erfan

0

我发现这个方法可行。它可以去除填充和边距,并且使图像大小适应屏幕宽度。

val warppedHtml = "<style>body { margin:0; padding:0;} img{display: inline;height: auto;max-width: 100%;}</style>${html}"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接