Android target35适配之窗口边衬区变更

targetsdk升级到35后,发现页面的toolbar和navigationBar 会遮挡页面原有的元素,主要是因为Google将安全区域变更了,原来布局会自动避开statusBar,而如今根布局相对于statusBar的偏移没有了,这对于非全屏的页面来说,就需要统一进行适配了。同样的,底部navigationBar的偏移也没有了,那么页面底部的导航栏可能会遮挡页面的元素。

Activity的onCreate方法中,加入如下的代码

java 复制代码
final View decorView = getWindow().getDecorView();
ViewCompat.setOnApplyWindowInsetsListener(decorView, new OnApplyWindowInsetsListener() {
            @Override
            public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
                v.setPadding(insets.getSystemWindowInsetLeft(),
                        insets.getSystemWindowInsetTop(),
                        insets.getSystemWindowInsetRight(),
                        insets.getSystemWindowInsetBottom());
                return insets;
            }
        });

参考文档:

1 https://developer.android.google.cn/about/versions/15/behavior-changes-15?hl=zh-cn#window-insets

相关推荐
百锦再3 小时前
React编程高级主题:测试代码
android·前端·javascript·react.js·前端框架·reactjs
2501_916008894 小时前
全面介绍Fiddler、Wireshark、HttpWatch、SmartSniff和firebug抓包工具功能与使用
android·ios·小程序·https·uni-app·iphone·webview
玉梅小洋5 小时前
Windows 10 Android 构建配置指南
android·windows
Libraeking6 小时前
视觉篇:Canvas 自定义绘图与高级动画的华丽圆舞曲
android·经验分享·android jetpack
Fushize7 小时前
多模块架构下的依赖治理:如何避免 Gradle 依赖地狱
android·架构·kotlin
Jomurphys7 小时前
Kotlin - 类型别名 typealias
android·kotlin
Haha_bj8 小时前
Flutter ——flutter_screenutil 屏幕适配
android·ios
Haha_bj8 小时前
Flutter ——device_info_plus详解
android·flutter·ios
前端小伙计8 小时前
Android/Flutter 项目统一构建配置最佳实践
android·flutter
Mr_sun.10 小时前
Day09——入退管理-入住-2
android·java·开发语言