android - 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尽可能多地占用窗口空间
===============================更新=================== ================
另一件事是,当显示键盘时,操作按钮不会添加新行,它只是某种确定按钮。
我想要一个非常简单的带有多行编辑文本的警报对话框......为什么这么难
解决方案
为 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();
推荐阅读
- python - 使用 Boto 自动上传到 AWS S3 存储桶 Yields Expired Token Error
- python - 使用 Opencv 进行人脸检测
- firebase - 具有多个字段的 Firestore 分布式计数器
- node.js - 向 mongoDB 插入数据时显示 db 未定义
- python - 使硒无法检测的措施
- sql - Visual Studio SQL 返回重复值
- spring - java_options 中的第二个弹簧配置文件
- javascript - 为什么在 JavaScript 对象的 getter 和 setter 上使用 delete?
- javascript - 原生 JS 使用每个父类上的计数器重置添加类
- swift - 查找并更新具有关联值的 Swift 枚举数组