2012-12-11 77 views
1

我想要一個完全自定義的提醒框簡而言之,我希望我的背景圖像,我的自定義按鈕和我的自定義消息,目前即時通訊使用默認警報框只有我的消息,但我希望它是完全自定義警告框,正如我前面自定義提醒框

說請幫我,如果其可能 thnks :)

目前的代碼是這樣的,請分享示例代碼段: -

AlertDialog.Builder alertDialogBuilder3 = new AlertDialog.Builder(context); 
       alertDialogBuilder3.setTitle("Location Check"); 
       alertDialogBuilder3 
       .setMessage("Do you want to cancel loading?") 
       .setCancelable(false) 
       .setPositiveButton("Ok",new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog,int id) { 

         LAtestTab.this.finish(); 
        } 
       }) 
       .setNegativeButton("No",new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog,int id) { 
         // if this button is clicked, just close 
         // the dialog box and do nothing 
         dialog.cancel(); 
        } 
       }); 
       ; 

       AlertDialog alertDialog3 = alertDialogBuilder3.create(); 

       alertDialog3.show(); 
+0

查看本教程http://www.mkyong.com/android/android - 自定義對話框的例子/和谷歌搜索「alertdialog自定義」你會得到很多很好的例子和代碼片段。 –

+0

你能告訴我PLZ如何在中心調用佈局它正常工作,但對話框的位置不在中心 –

回答

6

這裏就是Java代碼....

exit.setOnClickListener(new OnClickListener() 
{ 
    @Override 
    public void onClick(View arg0) 
    { 
      final Dialog dialog1 = new Dialog(CatchTheCatActivity.this); 
      dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE); 
      dialog1.setContentView(R.layout.custom_alert); 

      Button yes = (Button) dialog1.findViewById(R.id.button1); 
      Button no = (Button) dialog1.findViewById(R.id.button2); 

      yes.setOnClickListener(new OnClickListener() 
      { 
       @Override 
       public void onClick(View v) 
       { 
        finish(); 
       } 
      }); 
      no.setOnClickListener(new OnClickListener() 
      { 
       @Override 
       public void onClick(View v) 
       { 
         dialog1.dismiss(); 

       } 
      }); 
      dialog1.show(); 
    } 
}); 

這裏是XML文件.. 。(custom_alert)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="110dp" 
    android:background="@drawable/bg" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" android:gravity="center"> 


     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="5dp" 
      android:src="@drawable/textmessage" /> 

    </LinearLayout> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:gravity="center|bottom" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/yes_button"/> 


     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="20dp" 
      android:background="@drawable/no_button" /> 

    </LinearLayout> 

</LinearLayout> 
+0

thnks mehul .... –

+0

可以告訴我plz如何在中心調用該佈局,它工作正常但對話框的位置不在中心 –

+0

在我的應用程序它工作完美 –

2

請參考此鏈接

http://www.mkyong.com/android/android-custom-dialog-example/

http://android-er.blogspot.in/2011/06/custom-alertdialog.html

,您可以直接從佈局吹氣創建你的看法,你只需要使用您的佈局XML文件的名稱和文件佈局的ID。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/dialog_layout_root" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
> 

然後你可以設置你與下面的建設者佈局:

LayoutInflater inflater = getLayoutInflater(); 
View dialoglayout = inflater.inflate(R.layout.dialog_layout, (ViewGroup) getCurrentFocus()); 
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setView(dialoglayout); 
+0

你能告訴我PLZ如何在中心調用該佈局它正常工作,但對話框的位置不在中心 –

+1

將此屬性寫入您的視圖android:layout_gravity =「center」 – Nirali