android输入法弹出调整布局与沉浸式状态栏冲突+fitSystemWindows()被弃用问题
16lz
2021-12-04
输入法弹出,界面自动响应
当输入法出现时,为确保系统将布局大小调整为可见,可使用清单的 元素中
android:windowSoftInputMode="adjustResize"。
而为使 adjustResize元素可以成功起作用,要在activity的根布局上添加fitsSystemWindows="true"。
输入法弹出响应与沉浸式状态栏冲突
但如果此时页面是沉浸式状态栏状态,则会发现沉浸式失效、状态栏颜色异常。原本延伸至状态栏的显示内容,被顶了下来。
本质上是fitSystemWindows与沉浸式显示的冲突,给显示内容与状态栏之间插入了系统组件的padding距离,手动置为0即可。
网上的推荐解决办法是,重写activity根布局的viewGroup组件的fitSystemWindows()方法。
@Override protected boolean fitSystemWindows(Rect insets) { insets.top = 0; return super.fitSystemWindows(insets); }
fitSystemWindows()方法,在高版本中被废弃
但是今天发现,在高版本中该fitSystemWindows()方法已被废弃,于是在源码方法注释中看到,推荐使用
dispatchApplyWindowInsets(WindowInsets)、
onApplyWindowInsets(WindowInsets)、
setOnApplyWindowInsetsListener(android.view.View.OnApplyWindowInsetsListener)等方法来替代。
在高版本新方法,dispatchApplyWindowInsets方法中,逻辑的相同实现
于是在新方法中重写 相同逻辑的实现、亲测可用:
@Override public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) { Insets b = null; b = Insets.of(0, -insets.getSystemWindowInsets().top , 0, 0); //使两个inset.top字段相消为0 Insets result = Insets.add(insets.getSystemWindowInsets(), b); WindowInsets.Builder builder=new WindowInsets.Builder(insets).setSystemWindowInsets(result); return super.dispatchApplyWindowInsets(builder.build()); } return super.dispatchApplyWindowInsets(insets); }
***********************************************************************************************************************************
参考资料:
https://developer.android.google.cn/training/keyboard-input/visibility
https://www.jianshu.com/p/94072850ba58
https://blog.csdn.net/u012006926/article/details/53521198
更多相关文章
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用
- python list.sort()根据多个关键字排序的方法实现
- Android面试系列文章2018之Android部分ContentProvider篇
- Android事件处理方法总结-Handler消息处理
- Android原生json类org.json.JSONObject初步使用
- 《Android移动应用基础教程》(Android(安卓)Studio)(第二版)黑马教程
- Android中数据存储——SharedPreferences存储数据
- 2011Android技术面试整理附有详细答案(包括百度、新浪、中科软等