Android下API 21以下版本中未显示工具栏

3
在API 21以下运行时,工具栏未显示。当前,我在API 19模拟器中运行此应用程序。此相同的布局在运行API 21及以上版本的设备上正常工作。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".pdfScreen.PdfActivity">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/pdf_activity_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize">

            </androidx.appcompat.widget.Toolbar>


        </com.google.android.material.appbar.AppBarLayout>

        <com.github.barteksc.pdfviewer.PDFView
            android:id="@+id/pdfView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <include
            android:id="@+id/include_pdf_menu"
            layout="@layout/pdf_activity_bottom_menu"/>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

以下是翻译的结果:

这是style.xml文件

 <style name="themeForApp" parent="Theme.AppCompat.Light.NoActionBar">
         Customize your theme here.
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColor">@color/textOnPrimary</item>
    </style>



    <style name="pdfViewTheme" parent="themeForApp.NoActionBar">
    </style>

这是 v21/style.xml 文件。

    <style name="themeForApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>


    <style name="pdfViewTheme" parent="themeForApp">
        <item name="android:windowFullscreen">true</item>
    </style>

这里是我的AndroidManifest.xml文件,我在其中将pdfViewTheme应用于我的活动

<activity android:name=".pdfScreen.PdfActivity" android:theme="@style/pdfViewTheme"/>

你是否在你的活动中将工具栏设置为ActionBar?setSupportActionbar(toolber) - Arrowsome
是的,我已经完成了。 - Rajesh kumar
问题应该出在PDFView中的android:layout_height="match_parent"。它覆盖了整个屏幕,包括工具栏。在API>=21中,由于工具栏的默认高度,这种情况不会发生。 - Gabriele Mariotti
1
@GabrieleMariotti 非常感谢您。你是完全正确的。如果你把这个评论写成答案,我会接受它。 - Rajesh kumar
1个回答

2
问题出在android:layout_height="match_parent"上。
  <com.github.barteksc.pdfviewer.PDFView
      android:layout_height="match_parent" />

这样,PDFView 在 API<21 的情况下会覆盖整个屏幕,包括 Toolbar
原因是 AppBar/Toolbar默认的高度,并且位于比其他元素更高的高度上。但在 API<21 上无法实现。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接