首次激活画中画模式后软键盘被Fragment遮挡问题求助
解决思路
以下是针对该软键盘遮挡问题的具体排查和解决方向:
重置窗口软键盘模式
在Activity的onPictureInPictureModeChanged回调中,当退出画中画模式时,手动重置windowSoftInputMode为adjustResize,并强制触发布局重绘,修复PIP切换导致的参数状态异常:@Override public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) { super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig); if (!isInPictureInPictureMode) { getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); getWindow().getDecorView().requestLayout(); } }修正DialogFragment窗口属性
确保DialogFragment未设置与adjustResize冲突的软键盘模式(如adjustPan),且未添加FLAG_FULLSCREEN等干扰布局调整的窗口标记。可在DialogFragment的onResume方法中重置窗口属性:@Override public void onResume() { super.onResume(); if (getDialog() != null && getDialog().getWindow() != null) { getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); } }手动监听软键盘并调整布局
绕过系统失效的adjustResize机制,通过监听布局变化计算软键盘高度,动态调整根布局的底部padding:View rootView = getContentView().getRootView(); rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Rect visibleFrame = new Rect(); rootView.getWindowVisibleDisplayFrame(visibleFrame); int screenHeight = rootView.getHeight(); int keyboardHeight = screenHeight - visibleFrame.bottom; // 判定软键盘是否弹出(阈值设为屏幕高度15%) if (keyboardHeight > screenHeight * 0.15) { rootView.setPadding(0, 0, 0, keyboardHeight); } else { rootView.setPadding(0, 0, 0, 0); } } });适配三星/小米定制ROM
- 在Activity的Manifest配置中添加
configChanges,减少PIP切换时的Activity重建概率:<activity android:name=".ChatActivity" android:windowSoftInputMode="adjustResize" android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation" /> - 检查设备是否开启了"全屏显示"或"沉浸式状态栏"等定制功能,此类功能可能与
adjustResize冲突,尝试关闭后测试。
- 在Activity的Manifest配置中添加
确认fitsSystemWindows属性
确保聊天界面的根布局设置了android:fitsSystemWindows="true",该属性会让View自动适配系统窗口(包括软键盘)的布局空间。若PIP切换后该属性状态异常,可在代码中强制重置:if (!rootView.getFitsSystemWindows()) { rootView.setFitsSystemWindows(true); }
内容的提问来源于stack exchange,提问作者Vadim Eksler
相关产品推荐
相关产品推荐

