gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:29:13 25 4
gpt4 key购买 nike

我想制作一个居中显示标题、消息和按钮的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com