Android 使用最新的WindowInset API显示软键盘时如何调整对话框布局 问题:

Android 使用最新的WindowInset API显示软键盘时如何调整对话框布局 问题:,android,android-layout,android-softkeyboard,android-bottomsheetdialog,windowinsets,Android,Android Layout,Android Softkeyboard,Android Bottomsheetdialog,Windowinsets,如何使用最新的WindowInset API调整我的对话框和软键盘之间的空间 我有一个带有编辑文本的对话。默认动画将显示“编辑文本”正下方的软键盘,该软键盘将覆盖“保存”按钮。在做了一些研究之后,我将这一行添加到我的BottomSheetDialog片段中 getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); 它成功了(如下图所示) 但显然,SOFT\u

如何使用最新的WindowInset API调整我的对话框和软键盘之间的空间


我有一个带有编辑文本的对话。默认动画将显示“编辑文本”正下方的软键盘,该软键盘将覆盖“保存”按钮。在做了一些研究之后,我将这一行添加到我的
BottomSheetDialog
片段中

getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
它成功了(如下图所示)

但显然,
SOFT\u INPUT\u ADJUST\u RESIZE
不受欢迎

   * @deprecated Call {@link Window#setDecorFitsSystemWindows(boolean)} with {@code false} and
   * install an {@link OnApplyWindowInsetsListener} on your root content view that fits insets
   * of type {@link Type#ime()}.
我不知道如何使用ApplyWindowInsetsListener上的新
来达到同样的效果。
这是我当前的
BottomSheetDialog
片段:

public class BottomSheetDialog extends BottomSheetDialogFragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

//      Adding this line works, but it's deprecated in API 30
//      getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
      
        getDialog().getWindow().setDecorFitsSystemWindows(false);
        view = inflater.inflate(R.layout.fragment_bottom_dialog_cash, container, false);
        view.setOnApplyWindowInsetsListener((v, insets) -> {
            Log.d("dialog", "onCreateView: ");
            Insets imeInsets = insets.getInsets(WindowInsets.Type.ime());
            v.setPadding(0,0,0,imeInsets.bottom);
            return insets;
        });
        return view;
    }
我在另一个片段中使用onclicklistener来显示此对话框。在另一个片段中编码

    @Override
    public void onItemClick(int position) {
        BottomSheetDialog dialog = new BottomSheetDialog();
        dialog.show(getParentFragmentManager(), "BottomSheetDialog");
    }
事实上,日志表明,当软键盘弹出时,侦听器从未被触发。
仅供参考,我正在关注和。

经过更多的研究,我发现如果我使用
viewBinding
并将
view
替换为
bind.getRoot()
,那么一切都可以正常工作。我不知道为什么(也许我应该在
onViewCreated
中使用,而不是在
onCreateView
?)中使用),但代码应该对有相同问题的人有所帮助

// NOTE: you have to set this in the activity instead of fragment.
getWindow().setDecorFitsSystemWindows(false);

// Only work with API30 or above!
bind.getRoot().setOnApplyWindowInsetsListener((v, insets) -> { 
    imeHeight = insets.getInsets(WindowInsets.Type.ime()).bottom;
    bind.getRoot().setPadding(0, 0, 0, imeHeight);
    return insets;
});
需要注意的是,setDecorFitsSystemWindows(false)表示应用程序(您)负责所有系统窗口,包括状态栏和导航栏

我还发现下面链接的教程非常有用,如果您想了解更多有关WindowInset和新动画的信息,请查看


对我来说,唯一能起作用的是设置BottomSheetDialog的样式以使用以下内容:

<style name="BottomSheetDialogTheme" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
  <item name="android:windowIsFloating">false</item>
  <item name="android:windowSoftInputMode">adjustResize|stateVisible</item>
</style>

假的
调整大小|状态可见