java - 如何以编程方式修改 customAlertDialog 的 LinearLayout 的高度和宽度?

标签 java android resize android-alertdialog android-linearlayout

我想制作一个居中显示标题、消息和按钮的 customAlertDialog,但我无法使 customAlertDialog 的标题和消息的大小采用父级的大小

这就是我的 customAlertDialog 目前的样子 1

LinearLayout layout = new LinearLayout(context);
            layout.setOrientation(LinearLayout.VERTICAL);
            layout.setBackgroundColor(Color.RED);
            layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            final TextView header = new TextView(context);
            final TextView body = new TextView(context);
            final SpannableString formatHeader = new SpannableString(title);
            final StyleSpan negrita = new StyleSpan(android.graphics.Typeface.BOLD);
            formatHeader.setSpan(negrita,0, title.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

            header.setText(formatHeader);
            header.setGravity(Gravity.CENTER);
            header.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
            header.setTextColor(Color.BLACK);
            body.setPadding(8, 0, 8, 10);

            body.setText(message);
            body.setGravity(Gravity.CENTER);
            body.setPadding(8, 0, 8, 0);
            body.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
            body.setTextColor(Color.BLACK);

            layout.addView(header);
            layout.addView(body);
            view = layout;
            break;

最佳答案

您可以更改 Dialog 的默认 LayoutParams 以使对话框全屏显示,如下所示:

Window window = yourDialog.getWindow();
if (window != null) {
  WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
  lp.copyFrom(window.getAttributes());
  //This makes the dialog take up the full width
  lp.width = WindowManager.LayoutParams.MATCH_PARENT;
  lp.height = WindowManager.LayoutParams.MATCH_PARENT;
  window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  window.setAttributes(lp);
}

关于java - 如何以编程方式修改 customAlertDialog 的 LinearLayout 的高度和宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57296442/

相关文章:

java - Hibernate 未使用 Oracle 12c 返回自动增量列的正确值

java - admob 智能横幅需要位于应用程序底部(通过 java)

javascript - 在窗口大小调整时重新加载 JS 文件

objective-c - 在 Monotouch 中调整大小并将图像保存到磁盘

android - 在android Edittext中实现autoSizeTextType

java - 子集和问题: Returning a Variant of the Required Subset

java - 将区域设置日期字符串转换为时间戳。 Java/安卓

java - 数组中包含最大值的元素的位置

java - 如何将视频从 URI 转换为 byte[]

android - 如何使用 RxJava 逃离这个回调 hell