简体   繁体   中英

how to remove extra space in android list alert dialog

I am trying to display sample alert dialog with single choice items and facing problem with extra space after end of the list.

Here is my code

final CharSequence[] items = {"Red", "Green", "Blue"};

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("Pick a color");
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(context, items[item], Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();

带有单个选项的示例警报对话框

Suggest me to solve this.

You can have your own dialog layout (see docs), but I'd suggest to leave it as is, as this dialog will look differently on HC/ICS/JB, so tweaking its look is quite bad idea as on other versions of android it may look different. It's just the way OS does it. Leave it is my advice.

I had been facing a similar issue when I had build the custom alert dialog. In order to address the issue, I had to use theme to get rid of remaining spaces. Following is the code that I had followed:

private Dialog getCustomDialog() {
      AlertDialog.Builder builder = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Panel);
      AlertDialog dialog = builder.create();
      dialog.setView(dialogView, 0, 0, 0, 0);
      return dialog;
    }

As per the documentation here the usage of android.R.style.Theme_DeviceDefault_Panel is followed:

This removes all extraneous window decorations, so you basically have an empty rectangle in which to place your content. It makes the window floating, with a transparent background, and turns off dimming behind the window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM