【问题标题】:How to remove the Android WebView print added margin?如何去除 Android WebView 打印添加的边距?
【发布时间】:2017-01-23 21:04:39
【问题描述】:

我们尝试通过 google cloud print 打印 webview 内容,但无论我们做什么,结果打印输出都会增加一些边距。

有没有办法去掉这个边距? 我们试过了:

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

然后

<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">

然后

mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

都没有用...

【问题讨论】:

  • 我不知道它是否会影响打印,但html{margin:0; padding:0;} 可以创造奇迹。
  • 这可能看起来很愚蠢,但是您是否也尝试过在样式属性中使用!important?在根节点上也一样,html,即html, body{ margin: 0 !important; padding:0 !important; }?为了以防万一,总是可以将border 设置为 0。或者,也许在你的 CSS 中试试这个:@page{ margin-left: 0px; margin-right: 0px; margin-top: 0px; margin-bottom: 0px; }
  • 我们都试过了,似乎边距与 html 无关,而是 webview 在尝试打印时添加的恒定边距

标签: android html webview android-webview google-cloud-print


【解决方案1】:

使用以下代码在打印 WebView 时去除边距。

@page{
margin-left: 0px;
margin-right: 0px;
margin-top: 0px;
margin-bottom: 0px;
}

【讨论】:

  • 谢谢!这为我修好了!
【解决方案2】:

使用它 *{margin:0px; padding:0px} 添加样式表并检查一次

  *{margin:0px; padding:0px}
    body,html{padding:0px;margin:0px;}
 <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">

【讨论】:

    【解决方案3】:

    默认情况下,HTML 网页的内边距和边距为 10 像素;您必须在 head 部分或 css 文件中设置:

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

    这对我有用。希望对你有帮助:)

    或者您可以尝试另一个:

    用这个替换你的标签:

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

    这里还有一个关于 web 视图中图像的提示:添加适合屏幕宽度的图像的样式。适用于所有屏幕尺寸:

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

    【讨论】:

      【解决方案4】:

      如果使用 css 不能解决您的问题,您可以尝试使用带有 fromHtml 的 TextView 而不是使用 webview:

      TextView myTextView = (TextView) view.findViewById(R.id.my_textview);
      Spanned textviewHtml;
      
      //Note : fromHtml needs a display flag as second argument from API 24
      if (Build.VERSION.SDK_INT >= 24) {
          textviewHtml= Html.fromHtml(yourHtmlHere, Html.FROM_HTML_MODE_COMPACT);
      }
      else {
          textviewHtml= Html.fromHtml(yourHtmlHere);
      }
      
      myTextView.setText(textviewHtml);
      

      更多关于fromHtml的选项可以参考https://developer.android.com/reference/android/text/Html.html

      希望这会有所帮助! ;-)

      【讨论】:

        猜你喜欢
        • 2014-05-11
        • 1970-01-01
        • 2021-07-24
        • 1970-01-01
        • 2017-03-11
        • 2016-09-06
        • 2013-05-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多