我正在使用PDFDocument类将RelativeLayout
在运行时填充的内容转换为 PDF。我知道如何为 1 页 PDF 执行此操作,但是,如果在运行时,RelativeLayout
没有页面 > 1,那么我不知道如何执行此操作。我想我必须拆分文本 - 但我不知道该怎么做。
这是XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Summary"
android:id="@+id/mainwrapper"
android:padding="@dimen/allpadding">
<TextView
android:id="@+id/heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/summary"
android:textStyle="bold"
android:textSize="@dimen/appbar_text_size"
app:fontFamily="sans-serif-condensed"
android:paddingStart="@dimen/allaroundmargin"
android:paddingEnd="0dp">
</TextView>
<Button
android:id="@+id/btnToPDF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View as PDF"
android:layout_alignParentEnd="true">
</Button>
<TextView
android:id="@+id/datetext"
android:text="date"
android:layout_marginTop="@dimen/allaroundmargin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/heading"
android:textStyle="italic"
android:paddingStart="@dimen/allaroundmargin"
android:paddingEnd="0dp"
app:fontFamily="sans-serif-condensed">
</TextView>
<ScrollView
android:id="@+id/allQuizzesSV"
android:layout_below="@id/datetext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bgsummary"
android:layout_marginTop="@dimen/allaroundmargin">
<RelativeLayout
android:id="@+id/wrapperAllQuiz"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/quiz1LL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/btn_quiz1"
android:paddingLeft="@dimen/allaroundmargin"
android:paddingStart="@dimen/allaroundmargin"
android:paddingEnd="@dimen/allaroundmargin"
android:paddingRight="@dimen/allaroundmargin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_activity_quiz1"
android:textColor="@color/appbarTcolor"
android:textStyle="bold"
android:textSize="@dimen/appbar_text_size"
app:fontFamily="sans-serif-condensed">
</TextView>
<RelativeLayout
android:id="@+id/correctanswersQuiz1"
android:layout_marginTop="@dimen/allaroundmargin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvCorrectQuestionsQuiz1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/correctquestions"
android:textColor="@color/appbarTcolor"
app:fontFamily="sans-serif-condensed">
</TextView>
</RelativeLayout>
<RelativeLayout
android:id="@+id/wronganswersQuiz1"
android:layout_marginTop="@dimen/allaroundmargin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:fontFamily="sans-serif-condensed">
<TextView
android:id="@+id/tvWrongQuestionsQuiz1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/wrongquestions"
android:layout_marginTop="@dimen/allaroundmargin"
android:textColor="@color/appbarTcolor"
app:fontFamily="sans-serif-condensed">
</TextView>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/quiz2LL"
android:layout_below="@id/quiz1LL"
android:layout_marginTop="@dimen/margin_small_between_btns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/btn_quiz2"
android:paddingLeft="@dimen/allaroundmargin"
android:paddingStart="@dimen/allaroundmargin"
android:paddingEnd="@dimen/allaroundmargin"
android:paddingRight="@dimen/allaroundmargin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_activity_quiz2"
android:textColor="@color/appbarTcolor"
android:textStyle="bold"
android:textSize="@dimen/appbar_text_size"
app:fontFamily="sans-serif-condensed">
</TextView>
<RelativeLayout
android:id="@+id/correctanswersQuiz2"
android:layout_marginTop="@dimen/allaroundmargin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvCorrectQuestionsQuiz2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/correctquestions"
android:textColor="@color/appbarTcolor"
app:fontFamily="sans-serif-condensed">
</TextView>
</RelativeLayout>
<RelativeLayout
android:id="@+id/wronganswersQuiz2"
android:layout_marginTop="@dimen/allaroundmargin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:fontFamily="sans-serif-condensed">
<TextView
android:id="@+id/tvWrongQuestionsQuiz2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/wrongquestions"
android:layout_marginTop="@dimen/allaroundmargin"
android:textColor="@color/appbarTcolor"
app:fontFamily="sans-serif-condensed">
</TextView>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/quiz3LL"
android:layout_below="@id/quiz2LL"
android:layout_marginTop="@dimen/margin_small_between_btns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/btn_quiz3"
android:paddingLeft="@dimen/allaroundmargin"
android:paddingStart="@dimen/allaroundmargin"
android:paddingEnd="@dimen/allaroundmargin"
android:paddingRight="@dimen/allaroundmargin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_activity_quiz3"
android:textColor="@color/appbarTcolor"
android:textStyle="bold"
android:textSize="@dimen/appbar_text_size"
app:fontFamily="sans-serif-condensed">
</TextView>
<RelativeLayout
android:id="@+id/correctanswersQuiz3"
android:layout_marginTop="@dimen/allaroundmargin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvCorrectQuestionsQuiz3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/correctquestions"
android:textColor="@color/appbarTcolor"
app:fontFamily="sans-serif-condensed">
</TextView>
</RelativeLayout>
<RelativeLayout
android:id="@+id/wronganswersQuiz3"
android:layout_marginTop="@dimen/allaroundmargin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:fontFamily="sans-serif-condensed">
<TextView
android:id="@+id/tvWrongQuestionsQuiz3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/wrongquestions"
android:layout_marginTop="@dimen/allaroundmargin"
android:textColor="@color/appbarTcolor"
app:fontFamily="sans-serif-condensed">
</TextView>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/quiz4LL"
android:layout_below="@id/quiz3LL"
android:layout_marginTop="@dimen/margin_small_between_btns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/btn_quiz4"
android:paddingLeft="@dimen/allaroundmargin"
android:paddingStart="@dimen/allaroundmargin"
android:paddingEnd="@dimen/allaroundmargin"
android:paddingRight="@dimen/allaroundmargin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_activity_quiz4"
android:textColor="@color/appbarTcolor"
android:textStyle="bold"
android:textSize="@dimen/appbar_text_size"
app:fontFamily="sans-serif-condensed">
</TextView>
<RelativeLayout
android:id="@+id/correctanswersQuiz4"
android:layout_marginTop="@dimen/allaroundmargin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvCorrectQuestionsQuiz4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/correctquestions"
android:textColor="@color/appbarTcolor"
app:fontFamily="sans-serif-condensed">
</TextView>
</RelativeLayout>
<RelativeLayout
android:id="@+id/wronganswersQuiz4"
android:layout_marginTop="@dimen/allaroundmargin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:fontFamily="sans-serif-condensed">
<TextView
android:id="@+id/tvWrongQuestionsQuiz4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/wrongquestions"
android:layout_marginTop="@dimen/allaroundmargin"
android:textColor="@color/appbarTcolor"
app:fontFamily="sans-serif-condensed">
</TextView>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/quiz5LL"
android:layout_below="@id/quiz4LL"
android:layout_marginTop="@dimen/margin_small_between_btns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/btn_quiz5"
android:paddingLeft="@dimen/allaroundmargin"
android:paddingStart="@dimen/allaroundmargin"
android:paddingEnd="@dimen/allaroundmargin"
android:paddingRight="@dimen/allaroundmargin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_activity_quiz3"
android:textColor="@color/appbarTcolor"
android:textStyle="bold"
android:textSize="@dimen/appbar_text_size"
app:fontFamily="sans-serif-condensed">
</TextView>
<RelativeLayout
android:id="@+id/correctanswersQuiz5"
android:layout_marginTop="@dimen/allaroundmargin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvCorrectQuestionsQuiz5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/correctquestions"
android:textColor="@color/appbarTcolor"
app:fontFamily="sans-serif-condensed">
</TextView>
</RelativeLayout>
<RelativeLayout
android:id="@+id/wronganswersQuiz5"
android:layout_marginTop="@dimen/allaroundmargin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:fontFamily="sans-serif-condensed">
<TextView
android:id="@+id/tvWrongQuestionsQuiz5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/wrongquestions"
android:layout_marginTop="@dimen/allaroundmargin"
android:textColor="@color/appbarTcolor"
app:fontFamily="sans-serif-condensed">
</TextView>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/quiz6LL"
android:layout_below="@id/quiz5LL"
android:layout_marginTop="@dimen/margin_small_between_btns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/btn_quiz6"
android:paddingLeft="@dimen/allaroundmargin"
android:paddingStart="@dimen/allaroundmargin"
android:paddingEnd="@dimen/allaroundmargin"
android:paddingRight="@dimen/allaroundmargin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_activity_quiz6"
android:textColor="@color/appbarTcolor"
android:textStyle="bold"
android:textSize="@dimen/appbar_text_size"
app:fontFamily="sans-serif-condensed">
</TextView>
<RelativeLayout
android:id="@+id/correctanswersQuiz6"
android:layout_marginTop="@dimen/allaroundmargin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvCorrectQuestionsQuiz6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/correctquestions"
android:textColor="@color/appbarTcolor"
app:fontFamily="sans-serif-condensed">
</TextView>
</RelativeLayout>
<RelativeLayout
android:id="@+id/wronganswersQuiz6"
android:layout_marginTop="@dimen/allaroundmargin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:fontFamily="sans-serif-condensed">
<TextView
android:id="@+id/tvWrongQuestionsQuiz6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/wrongquestions"
android:layout_marginTop="@dimen/allaroundmargin"
android:textColor="@color/appbarTcolor"
app:fontFamily="sans-serif-condensed">
</TextView>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/adUnitID1">
</com.google.android.gms.ads.AdView>
<!--adview-->
</RelativeLayout>
这是 createPDF() 的片段
private void creatPDF() {
PdfDocument mypdfdoc = new PdfDocument();
PdfDocument.PageInfo mypageinfo1 = new PdfDocument.PageInfo.
Builder(1200, 2010, 1).create();
//page 1
PdfDocument.Page mypage1 = mypdfdoc.startPage(mypageinfo1);
//measurements for page 1
int measureWidth = View.MeasureSpec.makeMeasureSpec(mypage1.getCanvas().getWidth(), View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(mypage1.getCanvas().getHeight(), View.MeasureSpec.EXACTLY);
//put content on page 1
View contentQ1 = findViewById(R.id.allQuizzesSV);
contentQ1.measure(measureWidth, measuredHeight);
contentQ1.layout(0, 0, measureWidth, measuredHeight);
contentQ1.draw(mypage1.getCanvas());
//finish page 1
mypdfdoc.finishPage(mypage1);
String path1 = this.getExternalFilesDir(null).getAbsolutePath();
File mFile = new File(path1,"QuizSummaryN.pdf");
try{
mypdfdoc.writeTo(new FileOutputStream(mFile));
}catch (IOException e){
e.printStackTrace();
Toast.makeText(this, "Something wrong: " + e.toString(), Toast.LENGTH_LONG).show();
}
mypdfdoc.close();
}
在图片中,我也描述了我的问题。
你能指出我正确的方向吗?非常感谢