在android中对齐AlertView中的文本

在android中对齐AlertView中的文本,android,Android,我的alertDialog中有一个4行文本,我想在不提供任何自定义布局的情况下将其居中对齐。有可能吗。您可以这样做。通过在代码中设置标准对话框消息文本元素的重心,将标准AlertView对话框居中对齐,如下所示: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Title"); builder.setMessage("Message"); builder.setPositiveButto

我的alertDialog中有一个4行文本,我想在不提供任何自定义布局的情况下将其居中对齐。有可能吗。

您可以这样做。通过在代码中设置标准对话框消息文本元素的重心,将标准AlertView对话框居中对齐,如下所示:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.show();
// the following line gets a handler to the message element inside the dialog.
TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
// and now all that is needed is changing the gravity ...
messageText.setGravity(Gravity.CENTER);

是的,当然有可能。但是不提供任何自定义布局是什么意思呢?我想他指的是android文档中说的“如果你想显示更复杂的视图,请查看名为“自定义”的框架布局并将视图添加到其中”我的问题是消息出现的默认AlertView。它默认为左对齐。我们是否可以使其居中对齐。我们是否可以像这样将处理程序设置为标题。据我所知,答案是否定的。如果确实需要对齐标题,解决方案是使用新的XML主题扩展AlertDialog,这将根据主题属性格式化新对话框。有关更多信息,请参阅