Android: 创建用于打印的文档
Android: Create document for printing
在我的应用程序中,我需要能够创建可打印文档的大量数据。
我发现我可以使用 Android 4.4 及更高版本中包含的打印服务。我可以创建一个 HTML 文档并打印它,或者在每页的 Canvas
上绘图。
Printing HTML Document or Printing Custom Document
但我的问题是,我有很多内容,需要正确格式化。如果我必须将大型 HTML 文档编写为字符串,那将是一件很麻烦的事情...我需要创建一个漂亮的格式并从我的应用程序中添加数据。
对我来说,理想的解决方案是制作一个布局文件,对其进行扩充,用数据填充 TextView 和 ListView,然后打印该视图。但是打印 HTML 文档只允许从 WebView
打印,我还没有找到在 Canvas
上添加 View
或 Layout
的任何方法(只有另一个顺便说一句,我认为这是因为 Canvas 是一个布局元素,可以包含在视图中但不包含其他布局元素)。
有没有办法打印我的视图或其布局?
如果没有,有没有一种方法可以创建比用字符串写出来更容易的 HTML 文档?或者把一个View
的所有内容绘制到一个Canvas
的方法?
实际上有一种方法可以将View
的所有内容添加到Canvas
。它是:
view.draw(canvas);
public void draw (Canvas canvas)
Added in API level 1 Manually render this view (and all of its
children) to the given Canvas. The view must have already done a full
layout before this function is called. When implementing a view,
implement onDraw(android.graphics.Canvas) instead of overriding this
method. If you do need to override this method, call the superclass
version.
Parameters
canvas The Canvas to which the View is rendered.
这实际上是我一直在寻找的方法。
在我的应用程序中,我需要能够创建可打印文档的大量数据。
我发现我可以使用 Android 4.4 及更高版本中包含的打印服务。我可以创建一个 HTML 文档并打印它,或者在每页的 Canvas
上绘图。
Printing HTML Document or Printing Custom Document
但我的问题是,我有很多内容,需要正确格式化。如果我必须将大型 HTML 文档编写为字符串,那将是一件很麻烦的事情...我需要创建一个漂亮的格式并从我的应用程序中添加数据。
对我来说,理想的解决方案是制作一个布局文件,对其进行扩充,用数据填充 TextView 和 ListView,然后打印该视图。但是打印 HTML 文档只允许从 WebView
打印,我还没有找到在 Canvas
上添加 View
或 Layout
的任何方法(只有另一个顺便说一句,我认为这是因为 Canvas 是一个布局元素,可以包含在视图中但不包含其他布局元素)。
有没有办法打印我的视图或其布局?
如果没有,有没有一种方法可以创建比用字符串写出来更容易的 HTML 文档?或者把一个View
的所有内容绘制到一个Canvas
的方法?
实际上有一种方法可以将View
的所有内容添加到Canvas
。它是:
view.draw(canvas);
public void draw (Canvas canvas)
Added in API level 1 Manually render this view (and all of its children) to the given Canvas. The view must have already done a full layout before this function is called. When implementing a view, implement onDraw(android.graphics.Canvas) instead of overriding this method. If you do need to override this method, call the superclass version.
Parameters
canvas The Canvas to which the View is rendered.
这实际上是我一直在寻找的方法。