2012-02-02 72 views
7

是否可以在Android警报对话框中显示多行标题?我尝试了一些在这里发布的解决方案,但都没有为我工作。我总是以标题显示3个点(...)字符串为标题。 任何示例代码或有关同样的工作示例将不胜感激。如何使用多行标题构建警报对话框?

+0

看看我的答案,并考虑标记为正确。 – Radu 2013-04-29 14:26:00

+0

请考虑我的回答,令人讨厌的是在SO上误导“正确”答案。 – Radu 2013-05-04 05:11:18

回答

1

的方式,否则你必须去自定义对话框。

+0

我用3行标题字符串试过只能显示2和第三个字符串没有显示。 – Manju 2012-02-02 05:27:43

+0

这个答案是不正确的,不应该被接受的答案。 – 2017-02-07 17:39:28

2

这是如果你使用的警告对话框,然后标题可以包含最大2线设置标题

AlertDialog.Builder builder = new AlertDialog.Builder(Class name.this); 
    builder.setTitle("Welcome to App,\n There are no App.\n Add a new data."); 
+4

我认为你不能在标题栏中放置2行以上,我尝试过你的示例,并且能够获得2行的标题,并且第三行不见了。如果第一个字符串超过30个字符怎么办? – Manju 2012-02-02 05:26:34

20

您需要使用builder.setCustomTitle():

AlertDialog.Builder builder = new AlertDialog.Builder(context); 
TextView textView = new TextView(context); 
textView.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur " + 
       "tincidunt condimentum tristique. Vestibulum ante ante, pretium porttitor " + 
       "iaculis vitae, congue ut sem. Curabitur ac feugiat ligula. Nulla " + 
       "tincidunt est eu sapien iaculis rhoncus. Mauris eu risus sed justo " + 
       "pharetra semper faucibus vel velit."); 
builder.setCustomTitle(textView); 

文档是在这里:AlertDialog.builder

enter image description here

+0

'setCustomTitle()'不是'AlertDialog.Builder'的一种方法。 – 2013-05-26 13:21:22

+0

抱歉,对不起,你是对的。 'setCustomTitle(String)'不存在,但是'setCustomTitle(View)',就像你用的那样。 – 2013-05-28 21:47:41

+0

是的,工作完美。谢谢!! – 2013-05-29 00:11:37

0

其实, “正确” 的答案在这里是错误的。事实证明,您可以在AlertDialog中将最大线数设置为2以上。这里有一个例子:

AlertDialog closePlayerDialog; 
......... 
Builder builder = new AlertDialog.Builder(this); 
builder.setMessage(getString(R.string.AskToClosePlayer)) 
     .setPositiveButton(R.string.Yes, dialogClickListener) 
     .setNeutralButton(R.string.NoJustCloseApp, dialogClickListener) 
     .setNegativeButton(R.string.NoContinue, dialogClickListener); 
closePlayerDialog = builder.create(); 
closePlayerDialog.setOnShowListener(new DialogInterface.OnShowListener() { 
    public void onShow(DialogInterface dialog) { 
     float textSize = 12.0f; 
     Button positive = closePlayerDialog.getButton(AlertDialog.BUTTON_POSITIVE); 
     positive.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize); 
     positive.setMaxLines(3); 
     Button neutral = closePlayerDialog.getButton(AlertDialog.BUTTON_NEUTRAL); 
     neutral.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize); 
     neutral.setMaxLines(3); 
     Button negative = closePlayerDialog.getButton(AlertDialog.BUTTON_NEGATIVE); 
     negative.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize); 
     negative.setMaxLines(3); 
    } 
}); 
closePlayerDialog.setCancelable(false);  
closePlayerDialog.show(); 

基本上你编辑AlertDialog的组件onShow,使用DialogInterface.onShowListener

+0

您未显示如何更改标题的行数,而是按钮的数量。没有'dialog.getTitleBar'或类似的东西? – 2013-05-26 13:26:56

+0

@ LuisA.Florit其实你是对的路易斯。因此,在这种情况下,您可以完全按照您的要求删除默认标题栏,然后将自定义文本视图放置在自定义布局的顶部。您要删除现有标题栏的行是:dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); – Radu 2013-05-27 08:13:16