打开软键盘时,始终会调整DialogFragment的大小

Ion*_*gru 5 keyboard android resize dialogfragment

我在全屏显示自定义DialogFragment时遇到一些问题.此对话框具有可滚动且具有autocompletetextview的内容.

最初显示的对话框顶部有一个边距 - 以编程方式设置为布局内容顶部的透明视图.一旦autocompletetextview被聚焦,该边距就会减少到0(从而产生对话框以全屏模式进入的错觉).此时键盘也显示出来.

目前,键盘缩小了对话框的大小,对话框上的按钮向上移动,位于键盘上方.这会产生一个问题,因为只要autocompletetextview失去焦点,对话框就会调整大小,并且由于键盘调整大小也会创建一个闪烁:它们隐藏了键盘 - >对话框在底部移动 - >对话框调整为全屏 - >对话框resisez到初始大小

当前创建的对话框:

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);

        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);

        if (!hasTitle()) {
            dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        }

        // the content
        //final RelativeLayout root = new RelativeLayout(getActivity());
        //root.setLayoutParams(
        //        new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        //dialog.setContentView(root);

        if (position != null) {
            WindowManager.LayoutParams windowParams = dialog.getWindow().getAttributes();
            windowParams.x = position.x;
            windowParams.y = position.y;
            dialog.getWindow().setAttributes(windowParams);
        } else {
            dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        }

        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(hasTitle() ? Color.WHITE : Color.TRANSPARENT));
        dialog.getWindow().setGravity(Gravity.TOP | Gravity.START);

        return dialog;
    }
Run Code Online (Sandbox Code Playgroud)

这部分工作,因为对话框没有填充宽度,它要小得多.

有些东西可以解决这个问题,但它会产生另一个问题:

@Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_DeviceDefault_DialogWhenLarge_NoActionBar);
    }
Run Code Online (Sandbox Code Playgroud)

但这会影响对话框的背景,我似乎无法从对话框创建方法中更改它.

Ion*_*gru 9

我终于找到了解决问题的方法.

对话框的创建:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);

    if (!hasTitle()) {
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    }

    if (position != null) {
        WindowManager.LayoutParams windowParams = dialog.getWindow().getAttributes();
        windowParams.x = position.x;
        windowParams.y = position.y;
        dialog.getWindow().setAttributes(windowParams);
    } else {
        WindowManager.LayoutParams attrs = dialog.getWindow().getAttributes();
        attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
        dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }

    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(hasTitle() ? Color.WHITE : Color.TRANSPARENT));
    dialog.getWindow().setGravity(Gravity.TOP | Gravity.START);

    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);    

    return dialog;
}
Run Code Online (Sandbox Code Playgroud)

片段的创建(我自定义对话框的样式 - 使用No_FRAME非常重要):

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // We customize the style of this dialog - we remove the frames of the dialogs and leave the drawing to the
    // onCreateView() method, and we use the custom theme for dialogs - this is a special theme which has no
    // Background and the status bar is left unchanged
    setStyle(DialogFragment.STYLE_NO_FRAME, R.style.Theme_Dialog);
}
Run Code Online (Sandbox Code Playgroud)

自定义主题(我重用我的所有样式,以便对话框看起来相同):

<!-- Custom theme for dialogs - this will use the same styling as the main theme, but will disable the background
 of the window, and it will also leave the status bar color unchanged -->
<style name="Theme.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <!-- We do not want to change the status bar color -->
    <item name="colorPrimaryDark">@color/transparent</item>
</style>
Run Code Online (Sandbox Code Playgroud)

现在我的对话框片段看起来像是全屏幕,当打开软键盘时它不再调整大小,从而在手动改变它的大小时产生闪烁效果.重要的是作品DialogFragment.STYLE_NO_FRAME.

我希望这能帮助其他有类似问题的人.

  • DialogFragment.STYLE_NO_FRAME起到了作用! (2认同)
  • 真是恐怖。相信我,救救我。从最近的两天开始我一直在苦思冥想。这个答案应得100票。我在任何地方都找不到“ STYLE_NO_FRAME”。 (2认同)