删除 Android WebView 中不需要的空白
我已经开始使用 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);
}
}
I have started developing an App using WebView. Actually I am loading an Image with Webview (I like to use the built-in zoom controls of the class). I can successfully load the Image but I can see some irritating white spaces. I can't find the way to remove it. My Images are 750 * 1000 in size. I have attached my code below.
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
默认情况下,webview 在正文中有一些边距/填充。如果您想删除该填充/边距,请覆盖正文标签并添加边距,如下所示:
By default webview has some margin/padding in body. If you want to remove that padding/margin then override body tag and add margin like:
如果您所说的空白位于右侧,您可以将其添加到网络视图中。
这应该会使空白消失,只要它位于滚动条所在的右侧。
If the white space you're talking about is on the right side, you can add this to the webview.
That should make the white space go away, provided it's on the right side where the scroll bar would be.
默认情况下,HTML 网页的内边距和边距为 10 像素;你必须在你的 head 部分或 css 文件中进行设置:
这对我来说很有效。
By Defualt HTML web pages have a padding and margin of 10px; You have to set in your head section or or css file:
That did the trick for me.
我的 WebView 中渲染的 HTML 内容周围有令人恼火的空白(尽管该内容中没有图像)。起初我认为这与上面提到的 CSS 问题有关。但是通过使用样式表来更改 HTML、BODY 等的背景,它看起来越来越像 WebView 周围的填充。
user3241873 上面展示了如何调整 WebView 本身的填充,但我发现当 Eclipse 创建的默认 Android 应用程序生成布局文件(RelativeLayout“容器”而不是“LinearLayout”)时,它将这些属性添加到relativelayout:
如果您在 res/values/dimens.xml 中查找这些填充值,您会发现它们设置为 16dp。当我更改为 0dp 时,它删除了难看的空白:)
如果有人用 WebView 替换默认的(Hello World)TextView,则父relativeLayout的填充会像房子里令人讨厌的亲戚一样粘在一起。
I had irritating white space bordering the rendered HTML content in my WebView (no images in that content though). At first I thought it was related to CSS issues as mentioned above. But by using a style sheet to change the background for HTML, BODY, etc. it looked more and more like padding around the WebView.
user3241873 shows above how to adjust the padding on the WebView itself, but what I found was when the default Android application created by Eclipse generated the layout file (RelativeLayout "container" rather than "LinearLayout"), it added these attributes to the RelativeLayout:
If you look up those padding values in res/values/dimens.xml, you'll see that they're set to 16dp. When I changed to 0dp, it removed the unsightly white space :)
If someone replaces the default (Hello World) TextView with a WebView, the padding for the parent RelativeLayout sticks around like an obnoxious relative in the house.
这是一个使用 html 模板进行测试的示例,然后您可以将“正文”文本替换为您想要的任何内容...在您的情况下,是图像的 html。您可以根据需要使用 css 来设置 html 的样式。希望这可以帮助您更多地了解如何使用本地资源。如果您需要更多帮助,请发表评论。
不容错过的关键行是
使用它作为模板:(assets/template.html)
像这样使用 css: (assets/style.css)
然后在你的 WebView 类中:(你的扩展 WebView 的类)
This is an example that you can test that uses a html template, then you can replace the "body" text with whatever you would like... in your case, the html for an image. You can use the css to style the html as needed. Hope this helps you understand more how to use local assets. Post a comment if you need more help.
One key line to not miss is
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,target-densityDpi=device-dpi" />
Use this as a template: (assets/template.html)
Use css like this: (assets/style.css)
Then in your WebView class: (your class that extends WebView)
使用 WebView 的
setScrollBarStyle
方法并提供View.SCROLLBARS_INSIDE_OVERLAY
参数。Use WebView's
setScrollBarStyle
method and provide parameter asView.SCROLLBARS_INSIDE_OVERLAY
.我有这个[这是我的activity.xml]
我刚刚删除了relativeLayout中的填充
我尝试了上面的所有解决方案,但它们对我来说效果不太好。
这个对我有用。
I had this [this is my activity.xml]
I just deleted the padding in the relativeLayout
I tried all the solutions above but they didn't go very well for me.
this one worked for me.
我真的不知道您指的是哪个空白(也许图像有填充等),但通常如果您在 WebView 中遇到任何布局问题,您会尝试通过提供适当的(内联)css 样式表来修复它们。
i don't really know which whitespace you are referring to ( maybe the image has padding or so), but usually if you have any layout issues in a WebView you would try to fix them by providing a proper (inline) css-stylesheet.
将 webview.xml 中的填充设置为 0dp
Set the padding in your webview.xml to 0dp
我已经拿到了。
这是我的逻辑代码,当应用程序打开网站时,您必须获取网络视图的大小,然后将其设置在高度
这里是我的代码
希望您能接受我的代码和我的答案:)适用于任何设备,甚至选项卡:)
I already got it .
here my logic code, When the application open the website you must get the size of your webview then set it on height
here my code
Hope you will take my code and my answer to :) works on any devices even tabs too :)
我的情况是,我的网络视图顶部有很大的填充。因此,设置scrollbarSize=“0dp”。在AS 2.2.3中
My case, top of my webview has big padding. So, set scrollbarSize="0dp". In AS 2.2.3
Kotlin 答案
例如,我以这种方式删除了 iFrame 和 WebView 之间的空间:
Kotlin Answer
For example, I removed space between iFrame and WebView in this way: