2010-11-26 95 views
18

我想將可繪製對象放置到對話框標題欄中。我嘗試了以下方法:如何將圖標放入自定義對話框的標題

final Dialog dialog = new Dialog(this); 
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON); 
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.some_icon); 
dialog.setTitle(R.string.my_dialog_title); 
dialog.setContentView(R.layout.my_dialog_layout); 
... 

該圖標未顯示,但標題稍微向右移動。看起來對話框爲drawable保留空間,但不繪製它。我嘗試了幾個不同的圖標(也來自android資源),但沒有工作。

回答

15

撥打電話setFeatureDrawableResource()show()

不知道爲什麼這個工程。 :)

+0

謝謝!可能是框架中的錯誤? – Tom 2010-11-26 12:24:09

+6

使用這個,你把它放在節目之前。 requestWindowFeature - > setContentView - > setFeatureDrawableResource – 2011-02-27 12:04:59

15

這裏是解決

final Dialog dialog = new Dialog(this); 
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON); 
dialog.setTitle(R.string.my_dialog_title); 
dialog.setContentView(R.layout.my_dialog_layout); 
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.some_icon); 
dialog.show(); 

如果你希望你的對話框樣子不是添加主題對話活動的一個如下

final Dialog dialog = new Dialog(this,AlertDialog.THEME_HOLO_LIGHT); 
1
setIcon(R.drawable.image_name) 
2

這裏是解決方案。按照食譜,你會有你的圖標!注:訂單是非常重要的...

 final Dialog yourDialog = new Dialog(YourClass.this); 
      yourDialog.requestWindowFeature(Window.FEATURE_LEFT_ICON); //must come BEFORE setContentView 
      yourDialog.setContentView(R.layout.yourDialog_layout); 
      yourDialog.setTitle("Your Title"); 
      yourDialog.setCancelable(true); 
      yourDialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.your_icon); //must come AFTER setContentView 
2

我得到它以不同的方式工作,感謝SmaïlHammour職位。

將在您的首選工具類此靜態方法:

public static void msgBox(String msg, String title, int type, final Context c){ 

    int theIcon = drawable.ic_dialog_alert; 

    switch(type){ 
    case YourToolClass.CONFIRMATION: 
     theIcon = drawable.ic_menu_help; 
     break;  
    case YourToolClass.INFO: 
     theIcon = drawable.ic_dialog_info; 
     break; 
    case YourToolClass.ALERT: 
    default: 
    } 

AlertDialog.Builder builder = new AlertDialog.Builder(c); 

    /* Here enters the .setIcon: */ 
builder.setMessage(msg) .setTitle (title) .setIcon(theIcon); 

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int id) { 
     /* */ 
    } 
}); 


AlertDialog dialog = builder.create(); 
dialog.show(); 

} 

要調用:

YourToolClass.msgBox("the main message goes here", "Test", getBaseContext()); 
3

您還可以延長Dialog類,像這樣:

public class CustomDialog extends Dialog { 

    public CustomDialog(Context context) { 
     super(context); 
     setTitle("Some Title"); 
     requestWindowFeature(Window.FEATURE_LEFT_ICON); 
     setContentView(R.layout.my_layout); 
    } 

    @Override 
    protected void onStart() { 
     setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.my_icon); 
     super.onStart(); 
    } 

即你在構造函數中準備窗口特徵,然後在其中設置具體的資源在onStart。

所以,在你的主代碼,你可以簡單地使用:

CustomDialog cd = new CustomDialog(getActivity()); 
    rd.show(); 
0

調用setFeatureDrawableResource llike這

dialog.show(); 
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.logo_1x); 

即調用dialog.show()後,在我的情況下完美工作..謝謝。 。:)

相關問題