カスタム ダイアログで PDF を表示したい。サービスから PDF を ByteArray としてフェッチします。ByteArray を PDFView オブジェクトに渡し、ダイアログを膨らませます。pdfview を除く、ダイアログ内のすべてのビューが表示されます。
これが私のアクティビティコードです:
val fileName = "filename.pdf"
val dialog = Dialog(this)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(true)
dialog.setContentView(R.layout.activity_pdf_view)
val pdfView = dialog.findViewById(R.id.pdfView) as PDFView
val closeBtn = dialog.findViewById(R.id.closeBtn) as ImageButton
val downloadBtn = dialog.findViewById(R.id.downloadBtn) as Button
pdfView.fromBytes(byteArray).load()
dialog.show()
以下は、xml activity_pdf_view.xml です。
<?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="match_parent"
android:background="@color/transparent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/ticketLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@drawable/bg_ticket_layout">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Ticket"
android:textSize="15sp"
android:textColor="@color/black"
android:layout_margin="25dp"/>
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="@id/title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="39dp"
android:layout_marginBottom="8dp"
android:layout_marginHorizontal="8dp"/>
<ImageButton
android:id="@+id/closeBtn"
android:layout_width="11dp"
android:layout_height="11dp"
android:background="@color/white"
android:contentDescription="@string/close"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_cancel_icon"
android:layout_marginTop="20dp"
android:layout_marginEnd="24dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="@+id/downloadBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/submit_btn_bg"
android:drawableStart="@drawable/ic_file_download_white"
android:text="@string/download_pdf"
android:textColor="@color/white"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="@id/ticketLayout"
app:layout_constraintStart_toStartOf="@id/ticketLayout"
app:layout_constraintTop_toBottomOf="@id/ticketLayout"
android:paddingHorizontal="19dp"
android:drawablePadding="10dp"
android:layout_marginTop="15dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
ビューは次のようになります。
コードをデバッグして、byteArray が空でないことを確認しました。閉じるボタンやダウンロード ボタンなどの他のすべてのビューは、期待どおりに正しく機能します。pdfview だけでは何も表示されません。
最初に AlertDialog.Builder や Dialog テーマのアクティビティなど、さまざまな方法を試しましたが、最終的にコードを可能な限り単純な方法に分解しました。相変わらず視界に変化なし。
ここで問題を見つけるのを手伝ってください。事前にどうもありがとう:)
アップデート:
問題は、親の ConstraintLayout に wrap_content を使用すると、スクリーンショットのようにこの小さなビューが表示されることです。しかし、match_parent を使用すると、PDFView が正しく表示されます (ただし、画面全体に引き伸ばされて見苦しくなります)。そこで、親制約レイアウトの高さをハードコーディングしました。インターネットで掘り下げたところ、親 ConstraintLayout が子レイアウトを描画するときに PDFView 内の PDF が読み込まれないことがわかりました。すべてのデータがロードされた後にレイアウトを再描画できる方法を誰かが知っていますか?