2012-02-02 76 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