-->

DialogFragment buttons pushed off screen API 24 an

2020-07-30 02:38发布

问题:

I am making a custom DialogFragment that displays a selectable list of data. The list is too long to fit on the screen without scrolling. For up to API 23, everything seems to work fine, but when I test on API 24+, the DialogFragment's button's are no longer visible. I looked at Missing buttons on AlertDialog | Android 7.0 (Nexus 5x), but that doesn't seem to apply because my buttons do show up when I reduce the amount of content in the list so that it all fits on the screen. How can I make my buttons visible?

My onCreateDialog() method:

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    final View dialogView = View.inflate(getContext(), android.R.layout.select_dialog_multichoice, null);

    builder.setView(dialogView)
            .setTitle(R.string.muscle_groups)
            .setMultiChoiceItems(Exercise.MUSCLE_GROUPS, selectionTrackingArray, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    ...
                }
            })
            .setPositiveButton(R.string.affirmative, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ...
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

    return builder.create();
}

Buttons appear to be pushed off the screen

Let me know if any more info is needed.

回答1:

Do you happen to set message by using setMessage() method of AlertDialog.Builder, although your sample code doesn't use it?

Because if you have a content that doesn't fit screen, setting a custom view and message at the same time to an alert dialog builder has a side effect as you described.

To solve this issue add your message to your custom view, and don't set message text using setMessage() method, dialog buttons will be visible.

Hope this helps.