简体   繁体   中英

AlertDialog only showing neutral button on Android < 7.0

I have made an AlertDialog which has three buttons (positive, negative, neutral) and it works fine when I test it on my phone (Android 7.0) but I had a couple friends (with Android 5.0 and 6.0) test it and they only had the neutral button showing.

Here is the code:

AlertDialog.Builder isSelfieBuilder = new AlertDialog.Builder(context);
isSelfieBuilder.setTitle("Choose picture orientation.");

// Set up the buttons
isSelfieBuilder.setPositiveButton("Selfie", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        //Do something
    }
});
isSelfieBuilder.setNeutralButton("Landscape", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        //Do something
    }
});
isSelfieBuilder.setNegativeButton("Portrait", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        //Do something
    }
});
final AlertDialog picTypeDialog = isSelfieBuilder.create();
picTypeDialog.show();

final Button positiveButton = picTypeDialog.getButton(AlertDialog.BUTTON_POSITIVE);
LinearLayout.LayoutParams positiveButtonLL = (LinearLayout.LayoutParams) positiveButton.getLayoutParams();
positiveButtonLL.width = ViewGroup.LayoutParams.MATCH_PARENT;
positiveButton.setLayoutParams(positiveButtonLL);

final Button neutralButton = picTypeDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
LinearLayout.LayoutParams neutralButtonLL = (LinearLayout.LayoutParams) neutralButton.getLayoutParams();
neutralButtonLL.width = ViewGroup.LayoutParams.MATCH_PARENT;
neutralButton.setLayoutParams(neutralButtonLL);

final Button negativeButton = picTypeDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
LinearLayout.LayoutParams negativeButtonLL = (LinearLayout.LayoutParams) negativeButton.getLayoutParams();
negativeButtonLL.width = ViewGroup.LayoutParams.MATCH_PARENT;
negativeButton.setLayoutParams(negativeButtonLL);

Anyone has any idea why I only get the neutral button in there? I tried removing the alignment part (everything after .show() ) but I got the same result. I am testing on an emulator with API 21 (5.0) and I get the same problem.

final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Choose picture orientation.")
               .setPositiveButton("Selfie", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //doSomething
                    }
                })
                .setNeutralButton("Landscape", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //doSomething
                    }
                })
                .setNegativeButton("Portrait", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //doSomething
                    }
                });
        AlertDialog dialog = builder.create();
        dialog.show();

So the way I "fixed" this is by disabling entirely:

AlertDialog picTypeDialog = isSelfieBuilder.create();
picTypeDialog.show();

And replacing it simply with

isSelfieBuilder.show();

It works fine on all devices now even though the spacing between the button is not perfect and I cannot edit the button layout without an AlertDialog .

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