barteksc/AndroidPdfViewer 的背景颜色
Background color of barteksc/AndroidPdfViewer
由于在 JetPack compose 之前我对 Android 没有任何经验,所以我无法弄清楚这一点。
我正在使用 barteksc/AndroidPdfViewer
,但由于这是一个旧库,我需要将其包装在 AndroidView()
可组合项中。这可以正常工作并正确显示 PDF。
@Composable
fun PDFView(
byteArray: ByteArray,
) {
AndroidView(
modifier = Modifier
.fillMaxSize(),
factory = { context ->
PDFView(
ContextThemeWrapper(context, R.style.PdfView), null
)
},
update = { pdfView ->
Log.d(TAG, "PDF view UPDATE called")
pdfView
.fromBytes(byteArray)
.autoSpacing(false)
.spacing(25)
.pageFitPolicy(FitPolicy.BOTH)
.load()
}
)
}
根据 lib 的文档,设置一些间距并添加背景颜色会导致 PDF 页面在视觉上分离。
设置 AndroidView 的修饰符背景不起作用。
所以我尝试像这样在 R.style.PDFView
上设置背景(尝试了几个选项):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="PdfView" parent="Theme.MyPDFView">
<item name="colorSurface">@color/blue</item>
<item name="background">@color/blue</item>
<item name="backgroundColor">@color/blue</item>
</style>
</resources>
但这仍然没有改变背景。
为了完整起见,Theme.MyPDFTheme
如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.MyPDFView" parent="Theme.MaterialComponents.DayNight">
<item name="android:statusBarColor">@color/background_material_light</item>
<item name="android:windowLightStatusBar">true</item>
<item name="android:navigationBarColor">@color/background_material_light</item>
<item name="android:windowLightNavigationBar">true</item>
</style>
</resources>
我也试过在 Theme.kt
中设置背景颜色
private val LightColorPalette = lightColors(
surface = Color.Green,
background = Color.Yellow,
但是PDFView的背景没有改变,是的,设备处于浅色模式。
如何设置此视图的背景?
您可以使用 setBackgroundColor
设置 PDFView
背景颜色:
factory = { context ->
PDFView(
context,
null
).also {
it.setBackgroundColor(Color.Transparent.toArgb())
}
},
然后 Modifier
设置的背景颜色将变为可见。完整示例:
AndroidView(
factory = { context ->
PDFView(
context,
null
).also {
it.setBackgroundColor(Color.Transparent.toArgb())
}
},
update = { pdfView ->
pdfView
.fromStream(pdfView.context.resources.openRawResource(R.raw.sample))
.autoSpacing(false)
.spacing(25)
.pageFitPolicy(FitPolicy.BOTH)
.load()
},
modifier = Modifier
.fillMaxSize()
.background(Color.Red)
)
由于在 JetPack compose 之前我对 Android 没有任何经验,所以我无法弄清楚这一点。
我正在使用 barteksc/AndroidPdfViewer
,但由于这是一个旧库,我需要将其包装在 AndroidView()
可组合项中。这可以正常工作并正确显示 PDF。
@Composable
fun PDFView(
byteArray: ByteArray,
) {
AndroidView(
modifier = Modifier
.fillMaxSize(),
factory = { context ->
PDFView(
ContextThemeWrapper(context, R.style.PdfView), null
)
},
update = { pdfView ->
Log.d(TAG, "PDF view UPDATE called")
pdfView
.fromBytes(byteArray)
.autoSpacing(false)
.spacing(25)
.pageFitPolicy(FitPolicy.BOTH)
.load()
}
)
}
根据 lib 的文档,设置一些间距并添加背景颜色会导致 PDF 页面在视觉上分离。
设置 AndroidView 的修饰符背景不起作用。
所以我尝试像这样在 R.style.PDFView
上设置背景(尝试了几个选项):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="PdfView" parent="Theme.MyPDFView">
<item name="colorSurface">@color/blue</item>
<item name="background">@color/blue</item>
<item name="backgroundColor">@color/blue</item>
</style>
</resources>
但这仍然没有改变背景。
为了完整起见,Theme.MyPDFTheme
如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.MyPDFView" parent="Theme.MaterialComponents.DayNight">
<item name="android:statusBarColor">@color/background_material_light</item>
<item name="android:windowLightStatusBar">true</item>
<item name="android:navigationBarColor">@color/background_material_light</item>
<item name="android:windowLightNavigationBar">true</item>
</style>
</resources>
我也试过在 Theme.kt
private val LightColorPalette = lightColors(
surface = Color.Green,
background = Color.Yellow,
但是PDFView的背景没有改变,是的,设备处于浅色模式。 如何设置此视图的背景?
您可以使用 setBackgroundColor
设置 PDFView
背景颜色:
factory = { context ->
PDFView(
context,
null
).also {
it.setBackgroundColor(Color.Transparent.toArgb())
}
},
然后 Modifier
设置的背景颜色将变为可见。完整示例:
AndroidView(
factory = { context ->
PDFView(
context,
null
).also {
it.setBackgroundColor(Color.Transparent.toArgb())
}
},
update = { pdfView ->
pdfView
.fromStream(pdfView.context.resources.openRawResource(R.raw.sample))
.autoSpacing(false)
.spacing(25)
.pageFitPolicy(FitPolicy.BOTH)
.load()
},
modifier = Modifier
.fillMaxSize()
.background(Color.Red)
)