如何在android中设置自定义对话框的标题,图标

时间:2012-11-22 05:49:24

标签: android

我有一个在android中扩展Dialog的类。它显示了自定义对话框。但是我无法设置icon&该对话框的标题。我怎么能这样做?

我的代码:

public class helpDialog extends Dialog implements OnClickListener {
    Button okButton;
    String Description;
    TextView text;
    public helpDialog(Context context, String desc) {
        super(context);
        Description=desc;

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        /** Design the dialog in main.xml file */
        setContentView(R.layout.custom);
        text=(TextView)findViewById(R.id.text);
        text.setText(Description);
        okButton = (Button) findViewById(R.id.submit);
        okButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        /** When OK Button is clicked, dismiss the dialog */
        if (v == okButton)
        dismiss();
    }
}

我的custom.xml:

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:textColor="#000000"
    android:gravity="center"
    android:text="Help alert for iDispatch"
    android:textSize="18dp" />

<Button
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="OK"
    android:textColor="#FFFFFF" />

1 个答案:

答案 0 :(得分:0)

使用这一个

private AlertDialog AskOption()
{
    AlertDialog myQuittingDialogBox =new AlertDialog.Builder(this) 
        //set message, title, and icon
        .setTitle("Confirm Delete") 
        .setMessage("Do you want to Delete Employee "+listview_array[1]+" ?") 
        .setIcon(R.drawable.dialog_warning)
        .setPositiveButton(resource.getString(R.string.yes), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) 
            { 
                if(dh.selectEmployeeTypeById(EmpId).size()>0)
                {
                    //CODE

                }
                dialog.dismiss();
            }   
        })
        .setNegativeButton(resource.getString(R.string.no), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        })
        .create();
        return myQuittingDialogBox;
}

自定义对话框一个小例子布局这是我的自定义对话框

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

<TextView android:id="@+id/img"
android:layout_width="fill_parent"
android:text="@string/selectjob"
android:gravity="center"
android:textSize="30dp"
android:textColor="#fff"
android:background="#203C56"
android:padding="10dp"
android:layout_height="wrap_content"/>

<ListView
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:cacheColorHint="#222000" />

<View
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="#000"/>

<LinearLayout android:id="@+id/lin00"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="horizontal">

<Button
 android:layout_weight="0.1"
 android:contentDescription="@string/ok"
 android:id="@+id/okBtn"
 android:layout_gravity="center"
 android:layout_margin="10dp"
 android:padding="10dp"
 android:textSize="25dp"
 android:background="@drawable/toolbar_background"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/ok"/>

 <Button
 android:layout_weight="0.1"
 android:contentDescription="@string/ok"
 android:id="@+id/selectBtn"
 android:layout_gravity="center"
 android:layout_marginTop="10dp"
 android:layout_marginBottom="10dp"
 android:padding="10dp"
 android:textSize="25dp"
 android:background="@drawable/toolbar_background"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/selectall"/>


 <Button
 android:layout_weight="0.1"
 android:contentDescription="@string/cancel"
 android:id="@+id/cancelBtn"
 android:layout_gravity="center"
 android:layout_marginTop="10dp"
 android:layout_marginBottom="10dp"
 android:padding="10dp"
 android:textSize="25dp"
 android:background="@drawable/toolbar_background"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/cancel"/>

 </LinearLayout>
 </LinearLayout>

这是代码调用对话框

 private void showPopUp(int eid)
 {
 final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
 helpBuilder.setTitle("");

 LayoutInflater inflater = getLayoutInflater();
 final View PopupLayout = inflater.inflate(R.layout.jobselection, null);
 helpBuilder.setView(PopupLayout);

 final AlertDialog helpDialog = helpBuilder.create();
  helpDialog.show();

 okbtn = (Button)PopupLayout.findViewById(R.id.okBtn);
 caneclbtn = (Button)PopupLayout.findViewById(R.id.cancelBtn);
 selectallbtn = (Button)PopupLayout.findViewById(R.id.selectBtn);
 clearallbtn = (Button)PopupLayout.findViewById(R.id.clearallBtn);

  jobList = (ListView)PopupLayout.findViewById(R.id.list);

  mylist = new ArrayList<HashMap<String, String>>();

  for(int i=0;i<Punchedjobs.size();i++)
  {
  map = new HashMap<String, String>();
  map.put("name", Punchedjobs.get(i));
  mylist.add(map);
  }
 sd = new SimpleAdapter(StaffTimeClock.this,mylist,R.layout.jobslist,
 new String[]{"name"},new int[]{R.id.jobText});
 jobList.setAdapter(sd);

 okbtn.setOnClickListener(new OnClickListener() {

 public void onClick(View v)
 {
 \\code

 }
 });

 caneclbtn.setOnClickListener(new OnClickListener()
 {
 public void onClick(View v)
 {
 helpDialog.dismiss();
 }
 });

 selectallbtn.setOnClickListener(new OnClickListener() {

 public void onClick(View v)
 {

 }
 );


 }