【问题标题】:Aligning text in AlertView in android在android的AlertView中对齐文本
【发布时间】:2012-01-29 08:15:23
【问题描述】:

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

【问题讨论】:

  • 是的,当然可以。但是不提供任何自定义布局是什么意思?
  • 我认为他指的是 android 文档,上面写着“如果您想显示更复杂的视图,请查找名为“自定义”的 FrameLayout 并将您的视图添加到其中”
  • 我的问题是针对显示消息的默认 AlertView。默认为左对齐。我们可以让它居中对齐吗?

标签: android


【解决方案1】:

你可以做到。通过在您的代码中设置标准对话框的消息文本元素的重力,将标准 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);

【讨论】:

  • 我们是否也可以像这样获取标题的处理程序。
  • 据我所知,答案是否定的。如果您确实需要对齐标题,解决方案是使用新的 XML 主题扩展 AlertDialog,这将根据主题属性格式化新对话框。更多信息请参考Right justify text in AlertDialog
猜你喜欢
  • 2011-12-26
  • 1970-01-01
  • 1970-01-01
  • 2015-08-23
  • 2017-06-02
  • 2017-11-06
  • 1970-01-01
  • 2012-02-16
相关资源
最近更新 更多