自定义AlertDialog会生成错误

时间:2014-05-24 09:14:42

标签: java android xml alertdialog android-alertdialog

我想使用以下发布的代码创建自定义alertDialog。但是,出于某种原因,eclipse用红色波浪线强调了方法setIconsetTitle。 我不知道代码中缺少什么?

Java_Code:

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:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

    <RelativeLayout 
        android:id="@+id/reportRealtiveLayout00"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_gravity="top|left">
          <ImageView
              android:id="@+id/reportDialogIconID"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/reporticon01"/>
          <TextView 
              android:id="@+id/reportDialogTitleID"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_toRightOf="@+id/reportDialogIconID"
              android:gravity="center"/>
    </RelativeLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:2)

您需要inflate layout并设置为AlertDialog

LayoutInflater layoutInflater = (LayoutInflater)yourActivity.this.
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View layout = layoutInflater.inflate(R.layout.yourlayout,null);

reportAlertDialog.setView(layout);

然后使用该特定布局

引用您的ImageviewTextView
ImageView img=(ImageView)layout.findViewById(R.id.reportDialogIconID);
TextView title=(TextView)layout.findViewById(R.id.reportDialogTitleID);

<强>更新

将图标图片Drawble设置为您的imageview

  img.setImageResource(R.drawable.yourimage);

将标题文字设置为标题TextView,如

  title.setTitle("Your Title);

答案 1 :(得分:1)

setIcon的参数错误

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);

编辑:

如果您要将图片设置为自定义ImageView

reportAlertDialogIcon.setImageResource(R.drawable.youriamge);

并将文字设置为TextView

reportAlertDialogTitle.setText("Your Title");