2011-07-14 33 views
0

我想問一下如果我可以在可點擊的對話框標題中製作圖標。 我使用以下幾行來設置對話框左側的標題「新建聯繫人」和右側的圖標。那麼,這個圖標(x5)可以以某種方式點擊嗎?我希望它的功能像一個關閉按鈕。對話框標題中的可點擊圖標

dialog.requestWindowFeature(Window.FEATURE_RIGHT_ICON);

 dialog.setTitle("New Contact     "); 
    dialog.setContentView(R.layout.pop_up_new_contact);//loads the layout we have already create pop_up_new_contact.xml 
    dialog.setFeatureDrawableResource(Window.FEATURE_RIGHT_ICON, R.drawable.x5);//adds an icon in the left of the Title. 
    dialog.setCancelable(true); 

回答

0

它最好創建一個自定義對話框。但是,它的糟糕的用戶體驗設計將它用作關閉按鈕,因爲Android用戶習慣於採取相同的行動。

0

它確實出現在我看來,這並不完全符合您的要求,但是如果您正在尋找一個後退按鈕。這將工作。

dialog.setButton(getText(R.string.cancel), new DialogInterface.OnClickListener() 
{ 
    public void onClick(DialogInterface dialog, int whichButton) { } 
}); 
1

我找到了解決方案!這就是我所做的,以防有人需要它。

@Override 
protected Dialog onCreateDialog(int id) {//creates the dialog 

final Dialog dialog = new Dialog(this); 

dialog.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);//adds in the dialog box a frame for the custom title 

@Override 
protected Dialog onCreateDialog(int id) {//creates the dialog 
    final Dialog dialog = new Dialog(this); 

    dialog.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);//adds in the dialog box a frame for the custom title 

    //dialog.setTitle("New Contact");//sets the title of the dialog box 

    dialog.setContentView(R.layout.pop_up_new_contact);//loads the layout we have already create pop_up_new_contact.xml 
    dialog.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_dialog_box);//sets our custom title(custom_title_dialog_box.xml) as the current title of the dialog box. The custom_title_dialog_box.xml contains a TextView with id: title_new_contact and an ImageView with id: x1_button. 

    dialog.setCancelable(true);//close the dialog box when the back key is pushed. 

    ImageView x1_button = (ImageView)dialog.findViewById(R.id.x1_button); 

    x1_button.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View View3) { 
     dialog.dismiss(); 
     } 
    }); 

    return dialog; 
} 

也許這不是最好的解決方案,但至少它的工作原理!

+0

我使用'AlertDialog'內'DialogFragment'和編譯器把我 'android.util.AndroidRuntimeException:不能與其他標題features' – murt

+0

結合自定義標題其實你可以使用現在 '新AlertDialog.Builder( ...)。setCustomTitle(R.layout.custom_title)' https://stackoverflow.com/a/9430855/2163045 – murt