如何为自定义绘制仪编程修改线安装的高度和宽度



我想制作一个 customAlertDialog,显示标题,消息和按钮以中心为中心,但是我无法使我的 customAlertDialog的标题和消息的大小和邮件的大小为父

这是我的customAlertDialog当前看起来像

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);
}

最新更新