Android AlertDialog 自适应大小:完整指南
要使 AlertDialog 自适应大小,您可以使用以下代码:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(view);
AlertDialog alertDialog = builder.create();
alertDialog.show();
// 设置 dialog 的大小
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.copyFrom(alertDialog.getWindow().getAttributes());
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
alertDialog.getWindow().setAttributes(params);
在这段代码中,我们首先使用 AlertDialog.Builder 创建一个 AlertDialog。然后,我们使用 setView 将视图设置为 AlertDialog 中的视图,并进行创建和展示。
接下来,我们使用 WindowManager.LayoutParams 设置 AlertDialog 的大小。在此,我们将它们设置为 WRAP_CONTENT,这意味着它们将自适应 AlertDialog 的内容大小。
最后,我们将这些参数设置为 AlertDialog 窗口的属性,以便它们适应 AlertDialog 的大小。
这将确保您的 AlertDialog 始终完美地适应其内容,提供最佳的用户体验。
原文地址: https://www.cveoy.top/t/topic/og7M 著作权归作者所有。请勿转载和采集!