- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Android 创建一个 Pdf 文件 PdfDocument类(class)。我从 XML 文件(ConstraintLayout 中的 LinearLayout)扩展布局,然后在运行时通过添加带有外部数据的新行来完成。 p>
问题是输出的 pdf 可能很长也可能很短。我想避免在扩充布局之前指定页面高度。但是,我找不到在将布局添加到页面之前测量布局高度的解决方案
这是实际的代码:
PdfDocument document = new PdfDocument();
//set page width and height
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(227,992, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
//inflating the layout
View v = getLayoutInflater().inflate(R.layout.rapportino, null);
//add data to layout
//...
//measure the layout, draw it and finish page
int measureWidth = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getWidth(), View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getHeight(), View.MeasureSpec.EXACTLY);
v.measure(measureWidth, measuredHeight);
v.layout(0, 0, measureWidth, measuredHeight);
v.draw(page.getCanvas());
document.finishPage(page);
//write to file..
有没有办法根据 View 的高度创建 pdf?
编辑:这是我正在膨胀的布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/rapportino_indirizzo_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:gravity="end"
android:textColor="#000000"
android:textSize="@dimen/rapportino_global_textsize" />
<!--
other textview, linearlayout and imageview
..
..
-->
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
最佳答案
所以,我用一个很奇怪的解决方案解决了。由于向 View 添加布局会导致计算高度,因此我在创建 pdf 的 Activity 中创建了一个不可见 View (具有 pdf 的宽度):
<!-- add this to your activity -->
<LinearLayout
android:visibility="invisible"
android:id="@+id/output_pdf_view"
android:orientation="vertical"
android:layout_width="226dp"
android:layout_height="wrap_content" />
然后我将膨胀的布局添加到不可见 View (在用外部数据填充它之后)。使用观察器,您可以获得计算时的高度。
//inflating the layout
View v = getLayoutInflater().inflate(R.layout.rapportino, null);
//add data to layout (rows, images etc.)
//...
//find the invisible view
LinearLayout invisibleOutputPdfView = findViewById(R.id.output_pdf_view);
//add the pdf layout and observe changes
invisibleOutputPdfView.addView(v);
invisibleOutputPdfView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//remove the listener
invisibleOutputPdfView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int height = invisibleOutputPdfView.getHeight();
createPDF(height, ..);
}
});
注意:出于某种原因,在生成 pdf 时,dp 和 pt 之间似乎存在 1:1 的比例。因此不需要转换。
关于android - PdfDocument - 在布局末尾剪切页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54686236/
我有 2 个视频文件(input.mp4,leadout.mp4)和一个图像(watermark.png)。我需要将第一个视频剪切到 54 秒,添加水印,然后将其与第二个视频合并。 我实现了这个调用
在 VBA 中,您可以在剪切和粘贴后直接进行复制和粘贴。我之所以问,是因为 for 循环中的 If 语句需要剪切一行数据,然后直接在下面复制该行。当我运行代码时,它会执行该剪切,但不会执行副本。我在网
我想在剪切/粘贴事件触发后调用函数。 例如,当用户通过键盘或鼠标(带有上下文菜单)在编辑器中剪切/粘贴时,我想计算用户输入的字符数。 用户执行剪切/粘贴后有什么方法可以调用我们自己的函数吗? 最佳答案
抱歉,标题不准确,但实际上我正在尝试简化方法: Docker service ls 变成这个: Docker service ls cutted 尝试使用-f,但不起作用。 也许有一些grep / s
我有一个带有三角形的彩色噪声正方形。 现在,我希望多边形像圣诞节的“ cookies 切割器”一样减少这种噪音。导致被多边形路径包围的三角形噪声。 如何裁剪与多边形边框重叠的所有像素,然后将其另存为
我正在 Prolog 中开发一个谓词,它有可能在它结束之前终止。 出于这个原因,我正在寻找类似于 return; 的命令。 (C++)。我用了 cut !但我怀疑它的字面意思以及它是否确实做了什么re
我的代码如下所示: ServiceHandler sh = new ServiceHandler(); // Making a request to url and g
在 Linux(Raspbian 发行版)中,我试图提取文件名的日期部分,当我直接将其输入终端时,该文件名有效(见下文)。 $ file1="access_point20140821.csv" $ e
我正在使用 vuetify,我想制作一个可滚动的 stepper在对话框中。 这是一个代码笔 https://codepen.io/anon/pen/OqWQdy 我在 v-stepper-items
我有一个小测试站点,其中一个 div 的宽度减小到 50%,当我们单击按钮时另一个 div 出现。这是我的 codepen 当您单击该按钮时,图像会调整大小。因为我正在使用:background-si
我必须编写一个脚本文件来剪切以下列并将其粘贴到新 .arff 文件中同一行的末尾。我想文件类型无关紧要。 当前文件: 63,male,typ_angina,145,233,t,left_vent_hy
如何拦截此类事件? 当用户尝试将一些文本粘贴到我的 EditText 中时,我需要添加一些逻辑我知道我可以使用 TextWatcher 但这个入口点对我不利,因为我只在粘贴的情况下需要拦截,而不是每次
我有一个简单的自定义无边框 NSWindow 子类,它具有圆角矩形形状。 在此窗口的内容 View 中,我添加了一个 NSScrollView。 如何让 NSScrollView 将其文档 View
所以这是我的代码,但是每次尝试剪切字符串“words”都失败了,它只是用 jsoup 收到的整个文本执行 TextView。 我只想剪切字符串的前 x 个单词。 public class main e
Action Bar Select All/Cut/Copy not showing for EditText in Alert dialog(picture 2),请帮助 编辑:代码是
如何检测 tinymce 上的右键单击删除?我通过 onPaste 事件检测到粘贴事件,但我卡在剪切删除和复制上。我知道有 onContextMenu 事件,但似乎没有保存菜单项的函数或变量。 有什么
我有两个 JTextAreas 并且想要实现剪切、复制和粘贴菜单项。我知道 JTextArea.cut 和其他方法,但无法弄清楚如何确定用户在何处(在哪个 JTextArea 中)选择了文本和/或放置
我想在两侧“剪切”我的页面,如下所示: http://i.stack.imgur.com/ngZrp.jpg 演示:https://jsfiddle.net/r2g0eyxf/3/ #left {
我有一个绝对位于另一个元素之上的元素。问题是背景元素有一点 JS 可以根据鼠标的移动在 Y 轴上旋转。不幸的是,我在 Safari 中发现了一个在 Firefox 或 Chrome 中没有出现的问题。
我正在编写一个脚本,在该脚本中,我采用名片设计并使用它生成一张纸,上面有十张卡片,以匹配打印临时卡片的模板。这里最棘手的部分是出血;它们会在中间重叠,所以我需要为每个都制作剪贴蒙版。 我想出了一个系统
我是一名优秀的程序员,十分优秀!