1

我想要一个完全自定义的警报框,简而言之,我想要我的背景图像,我的自定义按钮和我的自定义消息,目前我使用的是默认警报框,只有我的消息,但我希望它是完全自定义的警报框正如我之前所说

请帮助我,如果可能的话,请分享一个示例代码片段:)

目前的代码是这样的: -

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

2 回答 2

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>
于 2012-12-11T06:24:43.550 回答
2

参考这个链接

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

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

您可以直接从 Layout Inflater 创建您的视图,您只需要使用您的布局 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);
于 2012-12-11T06:18:18.693 回答