align AlertDialog buttons to center

前端 未结 9 1883
无人共我
无人共我 2020-12-06 09:09

I use this codes for Android (Java) programming:

public static MessageBoxResult showOk(
        Context context, String title, String message, String okMessa         


        
9条回答
  •  北海茫月
    2020-12-06 09:45

    If you want to have Positive And Negative Buttons at the same time (Large & Center), you can use something like this:

    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Title");
    alertDialog.setMessage("Message");
    
    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    
    alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                     dialog.dismiss();
                }
             });
    alertDialog.show();
    
    Button btnPositive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
    Button btnNegative = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
    
    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) btnPositive.getLayoutParams();
    layoutParams.weight = 10;
    btnPositive.setLayoutParams(layoutParams);
    btnNegative.setLayoutParams(layoutParams);
    

提交回复
热议问题