首页 > 解决方案 > Android alertdialog 调整自定义视图以匹配父级

问题描述

我正在尝试使用高度匹配整个对话框窗口的 EditText 创建一个 AlertDialog。是否可以在不创建自定义布局的情况下做到这一点?

我正在尝试的是:

final EditText inputImports = new EditText(getContext());
                    inputImports.setMaxLines(500);
                    inputImports.setInputType(TYPE_TEXT_FLAG_NO_SUGGESTIONS);

                    AlertDialog alert = new AlertDialog.Builder(this.getContext()).setTitle(R.string.importar).setMessage(R.string.importCustomList).setView(inputImports).setCancelable(false).setNegativeButton(R.string.cancel, null).create();
                    alert.setOnShowListener(dialog12 -> {
                        ((AlertDialog) dialog12).getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                        inputImports.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
                        inputImports.requestLayout()
                    });
                    alert.show();

这使得对话框窗口与手机高度匹配,但编辑文本仍显示为单行编辑文本

在此处输入图像描述

我希望edittext尽可能多地占用窗口空间

===============================更新=================== ================

另一件事是,当显示键盘时,操作按钮不会添加新行,它只是某种确定按钮。

我想要一个非常简单的带有多行编辑文本的警报对话框......为什么这么难

在此处输入图像描述

标签: androidandroid-alertdialog

解决方案


为 Edittext 添加 LayoutParams

final EditText inputImports = new EditText(getContext());
inputImports.setMaxLines(500);
inputImports.setInputType(TYPE_TEXT_FLAG_NO_SUGGESTIONS);
inputImports.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));

编辑

代替

   inputImports.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;

   inputImports.getLayoutParams().height =  ((AlertDialog) dialog12).getWindow().getDecorView().getHeight();

推荐阅读