Android PdfDocument-在版面末尾剪切页面

Android PdfDocument-在版面末尾剪切页面,android,pdf-generation,android-linearlayout,Android,Pdf Generation,Android Linearlayout,我正在使用Android类创建一个Pdf文件。我从一个XML文件(ConstraintLayout中的LinearLayout)扩展布局,然后在运行时通过添加带有外部数据的新行来完成 问题是输出的pdf可以很长也可以很短。我希望避免在扩大布局之前指定页面高度。但是,在将布局添加到页面之前,我无法找到测量布局高度的解决方案 这是实际代码: PdfDocument document = new PdfDocument(); //set page width and height PdfDocume

我正在使用Android类创建一个Pdf文件。我从一个XML文件(ConstraintLayout中的LinearLayout)扩展布局,然后在运行时通过添加带有外部数据的新行来完成

问题是输出的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..
有没有办法根据视图的高度创建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>

所以,我用了一个非常奇怪的解决方案。由于向视图添加布局会导致计算高度,因此我在创建pdf的活动中创建了一个不可见视图(宽度为pdf):


注:出于某种原因,在生成pdf时,dp和pt之间的比例似乎为1:1。所以不需要转换。

所以,我用一个非常奇怪的解决方案解决了这个问题。由于向视图添加布局会导致计算高度,因此我在创建pdf的活动中创建了一个不可见视图(宽度为pdf):


注:出于某种原因,在生成pdf时,dp和pt之间的比例似乎为1:1。因此不需要转换。

您是否尝试过
MeasureSpec.UNSPECIFIED
?是的,我将makeMeasureSpec方法应用于
v.getWidth()
v.getHeight()
。使用
MeasureSpec.UNSPECIFIED
返回0和0,使用
MeasureSpec.justice
返回1073741824和1073741824,这两个值似乎不可用!你能发布你的xml文件吗?我编辑了这个问题,谢谢!抱歉,我不记得LinearLayout处于约束状态。这仅仅是因为pdf没有获得利润(不知道为什么)。无论如何,即使我删除了约束布局,它也不起作用。您可以尝试将
android:layout\u height=“wrap\u content”
放入pdf容器,并删除
measure
layout
方法吗?您尝试过
MeasureSpec.UNSPECIFIED
吗?是的,我将makeMeasureSpec方法应用于
v.getWidth()
v.getHeight()
。使用
MeasureSpec.UNSPECIFIED
返回0和0,使用
MeasureSpec.justice
返回1073741824和1073741824,这两个值似乎不可用!你能发布你的xml文件吗?我编辑了这个问题,谢谢!抱歉,我不记得LinearLayout处于约束状态。这仅仅是因为pdf没有获得利润(不知道为什么)。无论如何,即使我删除约束布局,它也不起作用。您可以尝试将
android:layout\u height=“wrap\u content”
放入pdf容器中,然后删除
measure
layout
方法吗?
<!-- 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" />
//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, ..);
        }
    });