我想将一个drawable放入一个对话框标题栏。我尝试了以下方法:
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资源),但没有使用它们。
答案 0 :(得分:15)
在 setFeatureDrawableResource()
之后致电show()
。
不知道为什么会这样。 :)
答案 1 :(得分: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);
答案 2 :(得分: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();
答案 3 :(得分: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
答案 4 :(得分: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());
答案 5 :(得分:1)
setIcon(R.drawable.image_name)
答案 6 :(得分:0)
调用setFeatureDrawableResource llike this
dialog.show();
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.logo_1x);
即在调用dialog.show()之后在我的情况下完美地工作..谢谢.. :))