如何构建具有多行标题的警报对话框?
Android 警报对话框中是否可以有多行标题?我尝试了这里发布的几个解决方案,但没有一个对我有用。我总是以显示 3 个点 (...) 字符串的标题结束。 任何与此相关的示例代码或工作示例都将受到高度赞赏。
Is it possible to have a multi-line title in an Android alert dialog? I tried a couple of solutions posted here but none worked for me. I always end up with the title showing 3 dots (...) string for title.
Any sample code or working example regarding the same would be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要使用 builder.setCustomTitle():
文档位于: AlertDialog.builder
You need to use builder.setCustomTitle():
Documentation is here: AlertDialog.builder
在我看来,这是一个更干净的解决方案。
对话框主题:
标题样式:
在代码中:
PS:您还可以指定不同的数字,不一定是 3。或者尝试您想要的样式。
It seems to me that this is a cleaner solution.
Dialog theme:
Title style:
In code:
PS: You can also specify a different number, not necessarily 3. Or play around with the styles you want.
这是设置标题的方法
This is the way to set title
您可以在
onShowListener
中通过 ID 搜索 titleView:You can search the titleView by its ID within the
onShowListener
:如果您使用警报对话框,则标题最多可以包含 2 行,否则您必须使用自定义对话框。
If you are using alert dialog then title can contain maximum 2 line, else you have to go with custom Dialog.
实际上,这里的“正确”答案是错误的。事实证明,您可以在 AlertDialog 中将最大行数设置为 2 以上。下面是一个示例:
基本上,您可以使用
DialogInterface.onShowListener
编辑 AlertDialog 的组件onShow
。Actually the "correct" answer here is wrong. It turns out you can set maximum lines to more than 2 in AlertDialog. Here is an example:
Basically you edit the AlertDialog's components
onShow
, usingDialogInterface.onShowListener
.