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

Android 15中setOnApplyWindowInsetsListener代码报错:无法解析方法及符号

问题解决步骤

根源分析

你遇到的Cannot resolve method 'getInsets' in 'WindowInsetsCompat'和Cannot resolve symbol 'Type'错误,是因为使用的androidx.legacy:legacy-support-v4:1.0.0版本过低——这个旧版本的WindowInsetsCompat类并不包含getInsets(WindowInsetsCompat.Type)这类适配Android 15的新API。

具体修复操作

1. 更新依赖库

打开Module级别的build.gradle,删除旧的legacy-support-v4依赖,替换为适配API 35的AndroidX Core库:

// 删除该行旧依赖
// implementation 'androidx.legacy:legacy-support-v4:1.0.0'
// 添加适配API35的Core库(版本选1.12.0及以上均可)
implementation 'androidx.core:core:1.12.0'

2. 修正导入语句

将系统原生的Insets导入替换为AndroidX兼容版本:

// 替换该行
// import android.graphics.Insets;
import androidx.core.graphics.Insets;

3. 同步并重建项目

在Android Studio中点击Sync Project with Gradle Files,之后依次执行Build > Clean Project和Build > Rebuild Project,确保依赖更新和代码生效。

验证代码

修正后Insets处理代码可正常编译:

ViewCompat.setOnApplyWindowInsetsListener(gridView, (v, windowInsets) -> {
    Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
    ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
    mlp.leftMargin = insets.left;
    mlp.bottomMargin = insets.bottom;
    mlp.rightMargin = insets.right;
    v.setLayoutParams(mlp);
    return windowInsets;
});

内容的提问来源于stack exchange,提问作者Mario S

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 22:13:14