开发者

Customize AlertDialog Footer

开发者 https://www.devze.com 2023-03-29 00:30 出处:网络
I want to customize the footer of the AlertDialog, I mean, the place where the buttons are located. I tried creating a separate layout and inflating it before calling builder = AlertDialog.Builder();

I want to customize the footer of the AlertDialog, I mean, the place where the buttons are located.

I tried creating a separate layout and inflating it before calling builder = AlertDialog.Builder();

like this:

AlertDialog.Builder builder;
        AlertDialog alertDialog;

        Context mCo开发者_运维百科ntext = activity;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.customdialog, (ViewGroup) activity.findViewById(R.id.layout_root));
        TextView dialogTitle = (TextView) layout.findViewById(R.id.dilaogTitle);
        dialogTitle.setText("Alert Dialog Test");

        TextView closeConfirmationQuestion = (TextView) layout.findViewById(R.id.textview);
        closeConfirmationQuestion.setText("Test for AlertDialog");


        builder = new AlertDialog.Builder(activity).setCancelable(false).setPositiveButton("Yes"), new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
                //Do something
            }
        }).setNegativeButton("No"), new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
                dialog.cancel();
            }
        });
        builder.setView(layout);
        alertDialog = builder.create();
        alertDialog.setView(layout, 0, 0, -3, -3);
        alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        alertDialog.show();

But I could customize only the content of my AlertDialog, but not the footer and the buttons. Here is how it is:

Customize AlertDialog Footer

I need to know how to customize the buttons and its holder, I mean, the footer of the AlertDialog, maybe put a color or image as its background instead of the standard 'grey' color, and also, set a customized background for the buttons.

Thanks.


If you wish to change the AlertDialog away from its designed format, I would recommend extending your own version of the Dialog and using it.


You could also use a new Activity and set the theme to android:theme="@android:style/Theme.Dialog" in the Android Manifest. This would allow you to use your own XML layout file.


In that case, why don't use custom dialog instead of alertdialog?


The setNegativeButton() and setPositiveButton() methods place the buttons at pre-defined locations. This is the reason that you are able to customize everything but the placement of buttons when you inflate a layout onto an AlertDialog.

Your options are:

  1. Do not use the setNegativeButton() or such methods - instead, add your own custom buttons in your layout. This means you would need to listen for specific button clicks. Somehow, this solution does not appeal to me since an AlertDialog is not meant to be used this way
  2. Use a custom dialog as mentioned in other answers (explained here).

@Phil s suggestion above looks interesting. I haven't tried it out though.


Also, while you are extending the Dialog, you may want to set it up so that it does not use the default title ( in order to have a consistent / fully customized look and feel ). This can be accomplished via:

" dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); "

0

精彩评论

暂无评论...
验证码 换一张
取 消