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

Android 15布局侵入系统栏且无TwoLineListItem却触发相关警告的问题咨询

Android 15布局侵入系统栏且无TwoLineListItem却触发相关警告的问题咨询

我最近在Android项目里碰到个头疼的布局问题:在Android 13及以下版本里一切正常,但升级到Android 15后,布局直接超出了触摸屏的正常边界,侵入了通知栏和导航栏区域,导致这部分区域的触摸操作完全失效,应用屏幕的顶部内容根本没法访问。

更奇怪的是,Android Studio还给我弹出了一条警告:

"TwoLineListItem is partially hidden in layout because it is not contained within the bounds of its parent in a preview configuration. Fix this issue by adjusting the size or position of TwoLineListItem."

但我的布局里完全没有TwoLineListItem,整个项目里都找不到这个控件的影子...

下面是我布局文件的开头部分:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:orientation="vertical">
    <View style="@style/Separator" />
    ...

补充尝试与临时解决方法

我试过参考一些方案但没能成功,不过后来自己摸索出了一个有效的解决办法:在setContentView之后调用这个方法:

fixTopPaddingAndroid15(getWindow(),findViewById(android.R.id.content));

对应的fixTopPaddingAndroid15函数实现如下:

public static void fixTopPaddingAndroid15(@NonNull Window w, @NonNull View v) {
    if (android.os.Build.VERSION.SDK_INT >= 35) {
        View decorView = w.getDecorView();
        decorView.post(() -> {
            WindowInsetsCompat insets = ViewCompat.getRootWindowInsets(decorView);
            if (insets != null) {
                ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
                params.topMargin = insets.getInsets(WindowInsetsCompat.Type.systemBars()).top;
                v.setLayoutParams(params);
            }
        });
    }
}

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.08 08:13:02