打开软键盘时,DialogFragment始终会调整大小 [英] DialogFragment is always resized when soft keyboard is opened

查看:177
本文介绍了打开软键盘时,DialogFragment始终会调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在全屏显示的自定义DialogFragment上遇到了一些问题.此对话框具有可滚动的内容,并具有自动完成的文本视图.

I'm having some issues with an custom DialogFragment that is displayed Fullscreen. This dialog has content that is scrollable and has an autocompletetextview.

最初,对话框显示在顶部,并在页面顶部以边距显示-通过编程将其设置为布局内容顶部的透明视图.一旦对autocompletetextview进行了聚焦,该边距将减小为0(这样就给人一种对话框进入全屏模式的错觉).此时,键盘也会显示出来.

Initially the dialog is displayed with an margin at the top - set programmatically as an transparent view at the top of the layout content. As soon as the autocompletetextview is focused, this margin is reduced to 0 (thus giving the illusion that the dialog is getting in fullscreen mode). At this moment the keyboard is also showed.

当前,键盘减小了对话框的大小,并且对话框上方的按钮在键盘上方向上移动.这就产生了一个问题,因为一旦autocompletetextview失去焦点,对话框便会被调整大小,并且由于键盘的大小也会产生闪烁: 他们的键盘是隐藏的->对话框在底部移动 ->对话框大小调整为全屏 ->对话框大小恢复为初始大小

Currently, the keyboard reduced the size of the dialog and an button that is on the dialog is moved up, above the keyboard. This creates an issue, because as soon as the autocompletetextview looses focus, the dialog is resized and because of the keyboard resize also an flicker is created: they keyboard is hidden -> the dialog is moved at the bottom -> the dialog resizes to fullscreen -> the dialog resisez to initial size

对话框的当前创建:

@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;
    }

这部分起作用,因为对话框没有填充宽度,所以要小得多.

This works partially, as the dialog is not filling the width, it is a lot smaller.

某些方法确实可以解决此问题,但会带来另一个问题:

Something does fix this, but it creates another issue:

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

        //setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_DeviceDefault_DialogWhenLarge_NoActionBar);
    }

但这会影响对话框背景的外观,我似乎无法通过对话框创建方法对其进行更改.

But this will affect how the background of the dialog looks like and I can't seem to be able to change it from the dialog creation method.

推荐答案

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

I have finally found an solution to my problem.

对话框的创建:

@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;
}

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

The creation of the fragment (i customize the style of the dialog - it is very important to use 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);
}

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

The custom theme (I reuse all my styles, so that the dialog looks the same):

<!-- 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>

现在,我的对话框片段看起来像是FullScreen,并且在打开软键盘时不会再调整大小,因此在手动更改其大小时会产生闪烁效果. 重要的是DialogFragment.STYLE_NO_FRAME.

Now my dialog fragment looks like it is FullScreen and it doesn't resize anymore when the soft keyboard is opened, thus creating the flickering effect when manually changing it's size. The important piece was the DialogFragment.STYLE_NO_FRAME.

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

I hope this will help others with similar problems.

这篇关于打开软键盘时,DialogFragment始终会调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆