【问题标题】:How to modify the height and width of a LinearLayout programmatically for a customAlertDialog?如何以编程方式为 customAlertDialog 修改 LinearLayout 的高度和宽度?
【发布时间】:2019-12-09 07:24:22
【问题描述】:

我想制作一个 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;

【问题讨论】:

    标签: java android resize android-alertdialog android-linearlayout


    【解决方案1】:

    您可以更改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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      • 2016-10-27
      • 2011-03-09
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      • 2018-02-16
      相关资源
      最近更新 更多