【问题标题】:Background color of barteksc/AndroidPdfViewerbarteksc/AndroidPdfViewer的背景颜色
【发布时间】:2022-01-28 13:38:38
【问题描述】:

由于我在编写 JetPack 之前没有任何 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 的背景没有改变,是的,设备处于光照模式。 如何设置此视图的背景?

【问题讨论】:

  • 您可以在 PDFView 中使用 setBackgroundColor

标签: android android-jetpack-compose android-jetpack


【解决方案1】:

你可以用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)
)

【讨论】:

    猜你喜欢
    • 2019-01-24
    • 2023-03-21
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多