Chr*_*SPE 2 html java printing android webview
我有一个java应用程序,我正在将其移植到android上。我的一项功能是将数据摘要导出为 HTML,并在本地浏览器中显示。我如何在 Android 平台上模仿此功能?目的是让用户轻松查看数据并根据需要保存/导出/打印。
这是我的常规桌面 Java 版本:
String html = "<html><head><style type=\"text/css\">.pagebreak {page-break-after: always;}.smallFont{font-size:10px}" + fancyCss + "</style>" + internationalCharacters + "</head><body>" + content + "</body></html>";
try {
File file = File.createTempFile(filename, ".html");
FileOutputStream stream = new FileOutputStream(file);
stream.write(html.getBytes("UTF-8"));
stream.flush();
stream.close();
Desktop.getDesktop().open(file);
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
简单的答案是使用 webview。
在你的 xml 文件中包含一个 webview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<WebView
android:id="@+id/myWebView"
android:layout_alignParentLeft="true"
android:layout_below="@+id/print_button"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:text="Print"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/print_button" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
您可以在 java 类中将 html 代码设置为 webview。
String html = "<html><body>Hello, World!</body></html>";
String mime = "text/html";
String encoding = "utf-8";
final WebView myWebView = (WebView)this.findViewById(R.id.myWebView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadDataWithBaseURL(null, html, mime, encoding, null);
Button printButton = (Button)findViewById(R.id.print_button);
printButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createWebPrintJob(myWebView);
}
});
Run Code Online (Sandbox Code Playgroud)
并使用以下方法进行打印作业
private void createWebPrintJob(WebView webView) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
PrintManager printManager = (PrintManager) this
.getSystemService(Context.PRINT_SERVICE);
PrintDocumentAdapter printAdapter =
null;
printAdapter = webView.createPrintDocumentAdapter("MyDocument");
String jobName = getString(R.string.app_name) + " Print Test";
printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
else{
Toast.makeText(MainActivity.this, "Print job has been canceled! ", Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
注意:上述方法仅适用于 Lollipop 或更高版本的设备。无论如何,如果需要,您可以将网络视图保存为位图。
该代码已经过测试并且可以工作。希望这对您有帮助
| 归档时间: |
|
| 查看次数: |
4356 次 |
| 最近记录: |