Java 自定义AlertDialog生成错误
我想使用下面发布的代码创建一个定制的alertDialog。但是,出于某种原因,eclipse用红色的波浪线强调方法setIcon和setTitle。 我不知道代码中缺少了什么 Java_代码:Java 自定义AlertDialog生成错误,java,android,xml,android-alertdialog,Java,Android,Xml,Android Alertdialog,我想使用下面发布的代码创建一个定制的alertDialog。但是,出于某种原因,eclipse用红色的波浪线强调方法setIcon和setTitle。 我不知道代码中缺少了什么 Java_代码: reportAlertDialog.setContentView(R.layout.report_dialog); ImageView reportAlertDialogIcon = (ImageView) reportAlertDialog.findViewById(R.id.r
reportAlertDialog.setContentView(R.layout.report_dialog);
ImageView reportAlertDialogIcon =
(ImageView) reportAlertDialog.findViewById(R.id.reportDialogIconID);
reportAlertDialog.setIcon(reportAlertDialogIcon);
TextView reportAlertDialogTitle =
(TextView) reportAlertDialog.findViewById(R.id.reportDialogTitleID);
reportAlertDialog.setTitle(reportAlertDialogTitle);
xml:
您需要将布局充气并设置为AlertDialog,如
然后使用特定的布局引用Imageview和TextView
ImageView img=(ImageView)layout.findViewById(R.id.reportDialogIconID);
TextView title=(TextView)layout.findViewById(R.id.reportDialogTitleID);
更新:
将图标image Drawble设置为您的imageview,如
将标题文本设置为标题文本视图,如
设置图标的参数错误
你有
reportAlertDialog.setIcon(reportAlertDialogIcon); // is a imageview
使用
也
这是错误的
public void setTitle (CharSequence title)
Added in API level 1
Set the title text for this dialog's window.
Parameters
title The new text to display in the title.
使用
编辑:
如果要将图像设置为自定义图像视图
和将文本设置为TextView
那么setcontentView不起作用了?我不是在质疑你的答案,而是在教程中使用了setContentView,而不是充气机!或者这是另一种选择?@Amr它会的。这与错误无关。@Amr这是另一种方法。我将简短地检查您的答案,提前谢谢
img.setImageResource(R.drawable.yourimage);
title.setTitle("Your Title);
public void setIcon (Drawable icon)
Added in API level 1
public void setIcon (int resId)
Added in API level 1
Set resId to 0 if you don't want an icon.
Parameters
resId the resourceId of the drawable to use as the icon or 0 if you don't want an icon.
reportAlertDialog.setIcon(reportAlertDialogIcon); // is a imageview
reportAlertDialog.setIcon(yourdrawable);
TextView reportAlertDialogTitle = (TextView) reportAlertDialog.findViewById(R.id.reportDialogTitleID);
reportAlertDialog.setTitle(reportAlertDialogTitle); // reportAlertDialogTitle is a textview object
public void setTitle (CharSequence title)
Added in API level 1
Set the title text for this dialog's window.
Parameters
title The new text to display in the title.
reportAlertDialog.setTitle("Your Title);
reportAlertDialogIcon.setImageResource(R.drawable.youriamge);
reportAlertDialogTitle.setText("Your Title");