Android AlertDialog自定义标题后消息无法居中,始终左对齐求助
Hey there! Let's sort out that AlertDialog message alignment problem you're having. You mentioned you've already set up a custom title, so here are two solid approaches to get your message centered properly:
If you want to stick with the default AlertDialog structure but just center the message, you can grab the system's built-in message TextView and update its gravity after showing the dialog:
// 先构建并显示对话框 String ScoreTitle = getResources().getString(R.string.AlertTitle); AlertDialog.Builder alrtbuilder = new AlertDialog.Builder(this); AlertDialog dialog = alrtbuilder.setTitle(ScoreTitle) .setMessage("你的对话框消息内容") .setPositiveButton("确定", (d, which) -> d.dismiss()) .create(); dialog.show(); // 获取系统默认的消息TextView并设置居中 TextView messageView = dialog.findViewById(android.R.id.message); if (messageView != null) { messageView.setGravity(Gravity.CENTER); // 可选:如果需要左右间距,添加这行 // messageView.setPadding(32, 16, 32, 16); }
This works because android.R.id.message is the standard ID the system uses for the AlertDialog's message text view. It's a quick fix without needing a full custom layout.
Since you're already inflating a custom layout (simple_dialog.xml), this is the most reliable way to control every part of the dialog's appearance, including centered text.
First, update your simple_dialog.xml to explicitly set gravity on your message text view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <!-- 你的自定义标题 --> <TextView android:id="@+id/custom_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:textStyle="bold" android:gravity="center" /> <!-- 居中的消息TextView --> <TextView android:id="@+id/dialog_message" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_marginTop="16dp" android:textSize="16sp" /> <!-- 可选:添加分隔线或其他自定义元素 --> </LinearLayout>
Then modify your Java code to populate the custom layout's views:
TextView vehicle_no = findViewById(R.id.vehicle_no); String ScoreTitle = getResources().getString(R.string.AlertTitle); AlertDialog.Builder alrtbuilder = new AlertDialog.Builder(this); Context mContext = alrtbuilder.getContext(); LayoutInflater mLayoutInflater = LayoutInflater.from(mContext); View mView = mLayoutInflater.inflate(R.layout.simple_dialog, null); // 设置自定义标题 TextView customTitle = mView.findViewById(R.id.custom_title); customTitle.setText(ScoreTitle); // 设置居中的消息内容 TextView dialogMessage = mView.findViewById(R.id.dialog_message); dialogMessage.setText("你的对话框消息内容"); // 绑定自定义布局到对话框 alrtbuilder.setView(mView); // 添加按钮逻辑 alrtbuilder.setPositiveButton("确定", (dialog, which) -> dialog.dismiss()); alrtbuilder.setNegativeButton("取消", (dialog, which) -> dialog.dismiss()); // 显示对话框 AlertDialog dialog = alrtbuilder.create(); dialog.show();
This approach gives you full control over spacing, font styles, and alignment—no reliance on system defaults, so it's more consistent across Android versions.
内容的提问来源于stack exchange,提问作者John

