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