可以看到底部的三个按钮不是居中的。Negative按钮偏右。可以做如下调整:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.CustomAlertDialog); AlertDialog dialog = builder.setTitle("My Title") .setMessage("My Message") .setPositiveButton("Positive", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { showToast("Positive"); } }).setNeutralButton("Neutral", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { showToast("Neutral"); } }).setNegativeButton("Negative", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { showToast("Negative"); } }).create(); dialog.show(); //一定要在dialog.show()之后才能执行下面的代码 Button mNegativeButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE); Button mPositiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE); Button mNeutralButton = dialog.getButton(AlertDialog.BUTTON_NEUTRAL); LinearLayout.LayoutParams positiveButtonLL = (LinearLayout.LayoutParams) mPositiveButton.getLayoutParams(); positiveButtonLL.weight = 1; mPositiveButton.setLayoutParams(positiveButtonLL); LinearLayout.LayoutParams mNegativeButtonLL = (LinearLayout.LayoutParams) mNegativeButton.getLayoutParams(); mNegativeButtonLL.weight = 1; mNegativeButton.setLayoutParams(mNegativeButtonLL); LinearLayout.LayoutParams mNeutralButtonLL = (LinearLayout.LayoutParams) mNeutralButton.getLayoutParams(); mNeutralButtonLL.weight = 1; mNeutralButton.setLayoutParams(mNeutralButtonLL);