2010-11-26 117 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()后,在我的情况下完美工作..谢谢。 。:)