首页 > 解决方案 > Android 全屏片段不显示导航和状态栏后面的元素

问题描述

我正在开发一款安卓游戏。它由一个主要活动和几个片段组成,通过它们的导航是通过导航组件处理的。我有一个小问题让我发疯。主要活动正确设置全屏,状态和按钮栏正确隐藏,我选择的背景图像完全全屏显示。相反,说到片段,它们的元素似乎隐藏在状态栏下方,或者布局的大小不符合屏幕的实际大小。如果我在布局的最底部放置一个按钮,如您在此屏幕截图中所见,屏幕上实际发生的情况(请注意,按钮被准确地剪切到按钮栏应该在的位置)。我使用android studio中提供的全屏片段作为模型,这些是其中使用的标志。

 int flags = View.SYSTEM_UI_FLAG_LOW_PROFILE
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;

我还尝试添加android:fitsSystemWindows="true"片段的根布局,但没有奏效。

编辑
问题是由非全屏的 FragmentContainerView 引起的。我尝试将背景设置为它而不是其父级,并且图像已被剪切:截图

主要的活动布局是:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/colourful_sound_waves_pattern_phone_wallpaper__1_"
    android:theme="@style/ThemeOverlay.LOCAL.FullscreenContainer"
    tools:context=".FullscreenActivity"
    android:id="@+id/frame_layout">
    <FrameLayout
        android:id="@+id/fullscreen_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:keepScreenOn="true"
        android:fitsSystemWindows="true">
        <androidx.fragment.app.FragmentContainerView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/nav_graph" />

        <LinearLayout
            android:id="@+id/fullscreen_content_controls"
            style="@style/Widget.Theme.LOCAL.ButtonBar.Fullscreen"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="bottom|center_horizontal"
            android:orientation="horizontal"
            tools:ignore="UselessParent">
        </LinearLayout>
    </FrameLayout>
</FrameLayout>

标签: androidandroid-layoutandroid-fragmentsandroid-viewandroid-fullscreen

解决方案


文档中所述,它通常用于容纳单个孩子,并且由于您正在计算线性布局运行时的高度,所以它没有得到它的获取高度。而不是第二个 Framelayout 标记,您应该使用 Constraintlayout 并将按钮添加为 linearlayout 的子项。像这样的东西:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/colourful_sound_waves_pattern_phone_wallpaper__1_"
android:theme="@style/ThemeOverlay.LOCAL.FullscreenContainer"
tools:context=".FullscreenActivity"
android:id="@+id/frame_layout">
<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/fullscreen_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:keepScreenOn="true"
    android:fitsSystemWindows="true">
    <androidx.fragment.app.FragmentContainerView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />

    <LinearLayout
        android:id="@+id/fullscreen_content_controls"
        style="@style/Widget.Theme.LOCAL.ButtonBar.Fullscreen"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_gravity="bottom|center_horizontal"
        android:orientation="horizontal"
        tools:ignore="UselessParent">
    </LinearLayout>
</FrameLayout>

推荐阅读