PAYATHON 2026

LinearLayout 内容显示到状态栏/顶栏后如何修复

数据小陈

先检查 Activity 的内容区域是否扩展到了系统栏或应用栏下面。LinearLayout 本身通常没有问题。接着确认主题有没有启用全屏、透明状态栏或 windowActionBarOverlay,并给根布局处理 WindowInsets。想快速验证,可以在根 LinearLayout 上加 android:fitsSystemWindows="true";生产代码里更推荐用 ViewCompat.setOnApplyWindowInsetsListener 设置系统栏 padding。

原因

  • 根布局使用了 match_parent,但没有给状态栏或导航栏留空间。
  • edge-to-edge 模式下,内容默认会画到系统栏后面;targetSdk 35 的 Android 15 应用也会遇到这种情况。
  • 主题若启用 windowActionBarOverlay 或透明状态栏,Button 也可能跑到 ActionBar 或状态栏下面。
  • 使用 AppBarLayout/Toolbar 时,内容布局要放在 AppBar 下方,或者加上 app:layout_behavior="@string/appbar_scrolling_view_behavior"。

快速验证

在根 LinearLayout 增加:

<LinearLayout
    ...
    android:fitsSystemWindows="true">

如果加上后内容正常,说明系统栏或应用栏的 inset 没有处理。要注意,在 edge-to-edge 或自定义 AppBar 的场景下,fitsSystemWindows 不一定能作为最终方案。

推荐修复:用 WindowInsets 给根布局加 padding

Java 示例:

LinearLayout root = findViewById(R.id.xmlActivityMain);

ViewCompat.setOnApplyWindowInsetsListener(root, (v, insets) -> {
    Insets bars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
    v.setPadding(bars.left, bars.top, bars.right, bars.bottom);
    return insets;
});

需要导入:

import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

如果根布局原本已有 padding,不要直接覆盖,要在原值上叠加:

int left = root.getPaddingLeft();
int top = root.getPaddingTop();
int right = root.getPaddingRight();
int bottom = root.getPaddingBottom();

ViewCompat.setOnApplyWindowInsetsListener(root, (v, insets) -> {
    Insets bars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
    v.setPadding(left + bars.left, top + bars.top, right + bars.right, bottom + bars.bottom);
    return insets;
});

检查主题和窗口设置

如果不需要全屏,确认没有误用:

<item name="android:windowFullscreen">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowActionBarOverlay">true</item>

尤其是 windowActionBarOverlay,它会让内容绘制到 ActionBar 后面。使用默认 ActionBar 时,通常应让内容位于 ActionBar 下方;使用自定义 Toolbar 时,则按 AppBarLayout 的结构处理 inset。

使用 AppBarLayout 时

把内容放进 CoordinatorLayout,并让内容避开 AppBar:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <com.google.android.material.appbar.MaterialToolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize" />
    </com.google.android.material.appbar.AppBarLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <!-- 原来的 Button/TextView -->
    </LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

注意事项

  • 不要同时对根布局和子布局重复加同一份 top inset,否则会出现双倍空白。
  • 根布局有背景时,通常把 padding 加在根布局上;如果只希望列表避让,可以加在具体的内容容器上。
  • android:textSize="20dp" 应改成 sp,例如 20sp;dp 不是推荐的字体单位。
  • 修改后在不同 Android 版本、手势导航/三键导航、横竖屏下各验证一次。

备注:内容仅供参考。