【问题标题】:Webview adding white space on right sideWebview在右侧添加空白
【发布时间】:2017-08-22 07:46:48
【问题描述】:

如何将具有固定高度和宽度的 html 内容加载到高度和宽度设置为 wrap_content 的 android webview 中? 这是我的 HTML 页面,即从资产加载的 label.html。

 <!doctype html>
 <html>
 <head>
 <meta charset="utf-8">
 <title>Untitled Document</title>
 <style>
 @charset "utf-8";
 /* CSS Document */
  .Main{
  width:50%;
  height:200px;
  border: 1px solid #09ED1C;
  float:left;
  background-color:#00F3FF;
  }
 H1{
 color:#FC1D21;
 }
</style>
</head>
<body>
<div class="Main" align="center">
<h1>This page is created using internal CSS</h1>
</div>
</body>
</html>



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue">

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

</RelativeLayout>

WebSettings webSettings = mWebViewHelp.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebViewHelp.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
mWebViewHelp.loadUrl("file:///android_asset/label.html");

这是在 webview 的右侧添加空白,即使它的 wrap_content,WebView.SCROLLBARS_INSIDE_OVERLAY 也没有解决我的问题,任何帮助将不胜感激。

【问题讨论】:

  • 为什么要设置 html 页面宽度 50%
  • html页面高度和宽度导致问题删除width:50%; height:200px;
  • @AkhileshPatil,它可以是任意高度和宽度。
  • 只需删除width:50%; height:200px;,它会自动在您的视图中调整

标签: android html css android-layout android-webview


【解决方案1】:

您需要在 HTML 标头中添加此内容:

    <meta name="viewport" content="width=device-width, initial-scale=1">

所以它看起来像这样:

 <!doctype html>
 <html>
 <head>
 <meta charset="utf-8">
 <title>[YOUR_DOCUMENT_NAME]</title>
 <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

我希望它有效,所以请不要回复

【讨论】:

  • 那你看到了什么?你能给我更多的信息吗?
  • 添加元标记后看起来像link,如果你能看到右侧的白色空间
  • 右边的空白是正常的,因为你将宽度设置为50%,但是左边和顶部的空白是不正常的。
  • 是的,但是宽度和高度可以是任何值。它将是自定义值。
  • 所以你想在顶部和左边获得空间。所以你的背景颜色填充到顶部和左侧?
猜你喜欢
  • 1970-01-01
  • 2018-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多