问题描述
我正在开发一款 Android 游戏。它由一个主要活动和几个片段组成,通过它们的导航是通过导航组件处理的。我有一个小问题让我发疯。主要活动正确设置为全屏,状态栏和按钮栏正确隐藏,我选择的背景图像完全全屏显示。相反,说到片段,它们的元素似乎隐藏在状态栏下方,或者布局的大小不符合屏幕的实际大小。如果我在布局的最底部放置一个按钮,如您在此 screenshot 中看到的那样,屏幕上实际发生的情况是 this(请注意,该按钮被准确切割到按钮栏的位置) .我使用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 以某种方式引起的。我尝试将背景设置为它而不是其父级,并且图像已被剪切:screenshot
主要活动布局是:
<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>
解决方法
正如document中提到的,它通常用于容纳单个孩子,并且由于您正在计算线性布局运行时的高度,因此它不会获得其获得的高度。 您应该使用 Constraintlayout 并添加按钮作为线性布局的子项,而不是第二个 Framelayout 标记。像这样:
<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>