You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android如何在无状态栏情况下将内容置于屏幕刘海下方?

问题描述

我尝试让应用内容完全显示在设备刘海下方,但不管怎么操作,状态栏始终会显示。

相关代码

MainActivity 代码

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER);

activity_main.xml 文件内容

<RelativeLayout 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="#72bcd4"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:textSize="24dp"
        android:background="#EC3BAB"
        android:text="Quizzer Gamer"
        android:paddingTop="14sp"
        android:paddingBottom="14sp"
        android:layout_alignParentTop="true"
        android:textColor="#ffffff"
        android:gravity="center"
        android:layout_centerHorizontal="true"  />

</RelativeLayout>

themes.xml 文件内容

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    </style>
</resources>
解决方法

你的代码存在两处关键错误:

  1. setFlags方法使用错误:该方法第一个参数是要设置的标志位,第二个是要清除的标志位,你将全屏标志和刘海布局模式标志混用,导致全屏设置不生效。
  2. 刘海布局模式选择错误:LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER会让内容避开刘海区域,若要让内容延伸到刘海下方,需使用LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES。

按以下步骤修改:

1. 修正MainActivity代码

替换原代码为:

import android.view.View;
import android.os.Build;
import android.view.WindowManager;

// ...

// 启用全屏模式
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 允许布局延伸到系统UI区域
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

// 适配Android P及以上的刘海屏
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    WindowManager.LayoutParams params = getWindow().getAttributes();
    params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
    getWindow().setAttributes(params);
}

2. 修改布局文件

在activity_main.xml的根布局中添加android:fitsSystemWindows="false",确保布局不被系统UI区域挤压:

<RelativeLayout 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="#72bcd4"
    android:fitsSystemWindows="false"
    tools:context=".MainActivity">

    <!-- 原有TextView内容不变 -->

</RelativeLayout>

内容的提问来源于stack exchange,提问作者Adam Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 15:45:39