将 AlertDialog 按钮居中对齐

align AlertDialog buttons to center

我将此代码用于 Android (Java) 编程:

public static MessageBoxResult showOk(
        Context context, String title, String message, String okMessage)
{
    okDialogResult = MessageBoxResult.Closed;

    // make a handler that throws a runtime exception when a message is received
    final Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message mesg)
        {
            throw new RuntimeException();
        }
    };

    AlertDialog.Builder alert = new AlertDialog.Builder(context);
    alert.setTitle(title);
    alert.setMessage(message);

    alert.setPositiveButton(okMessage, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int whichButton) {
            okDialogResult = MessageBoxResult.Positive;
            handler.sendMessage(handler.obtainMessage());
        }
    });

    AlertDialog dialog = alert.show();


    // align button to center
    Button b = (Button) dialog.findViewById(android.R.id.button1);
    b.setGravity(Gravity.CENTER_HORIZONTAL);

    // loop till a runtime exception is triggered.
    try { Looper.loop(); }
    catch(RuntimeException e2) {}

    return okDialogResult;
}

我的问题是如何使按钮居中?如您所见,我尝试使用 Gravity.CENTER_HORIZONTAL(也 .CENTER)将按钮与 cnenter 对齐,但没有任何变化。按钮几乎在正确的位置。

我假设您正在使用支持库中的 AlertDialog。

如果是这种情况,请尝试将导入替换为 android.app.AlertDialog。

这对我有用:

    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppCompatAlertDialogStyle);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });


    final AlertDialog dialog = builder.create();
    dialog.show(); //show() should be called before dialog.getButton().


    final Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
    LinearLayout.LayoutParams positiveButtonLL = (LinearLayout.LayoutParams) positiveButton.getLayoutParams();
    positiveButtonLL.gravity = Gravity.CENTER;
    positiveButton.setLayoutParams(positiveButtonLL);

使用crtn的方法,但是不是改变LayoutParam's重力,而是改变它的宽度为ViewGroup.LayoutParams.MATCH_PARENT;

使用android.support.v7.app.AlertDialog,这将使您的正负按钮居中对齐。

android.app.AlertDialog 会将按钮放在顶部,在底部留下 16dp space。

试过crtn的方法和Scott Brown的修改,都没有达到我喜欢的效果。

crtn 的解决方案根本没有改变按钮的外观(我正在使用 android.R.style.Theme_Material_Light_Dialog),Scott Brown 的解决方案使我的肯定按钮超出了父对话框的边缘。

对于 Theme_Material_Light_Dialog,按钮包含在 LinearLayout 子类中,该子类使用空白视图作为其第二个(索引 1)元素来向右推动按钮。

我像 crtn 一样抓取 Button ref:

AlertDialog dialog = bld.create();
dialog.show(); //show() MUST be called before dialog.getButton
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);

但后来我将 leftSpacer 设置为 View.GONE 并将父项的重力设置为 CENTER_HORIZONTAL

LinearLayout parent = (LinearLayout) positiveButton.getParent();
parent.setGravity(Gravity.CENTER_HORIZONTAL);
View leftSpacer = parent.getChildAt(1);
leftSpacer.setVisibility(View.GONE);

这样做的好处是不会破坏对话框的按钮堆叠行为。缺点是如果内部布局发生变化,就会break,所以YMMV.

如果你想同时有正负按钮(大号和居中),你可以使用这样的东西:

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);

您可以设置正负按钮和中性按钮,隐藏正按钮和中性按钮,并使用 LayoutParams 将负按钮放在中性按钮应该位于(中心)的位置。

在 onCreateView 中:

dialog = builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        })
                 .setPositiveButton(R.string.go_on, new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {

                     }
                 })
                 .setNeutralButton(R.string.do_nothing, new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {

                     }
                 })
                 .create();

在 onStart() 中:

 super.onStart();

    final Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
   positiveButton.setVisibility(View.INVISIBLE);
   final Button neutralButton = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
   neutralButton.setVisibility(View.INVISIBLE);
   final Button negativeButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
   negativeButton.setLayoutParams(neutralButton.getLayoutParams());

这是真正有用的东西。

3 个按钮(中性、正 ve 和负)的父级是 ButtonBarLayout,它扩展了 LinearLayout。要在 LinearLayout 中集中视图,重量、宽度和 layout_gravity(但不是重力)很重要,这些代码完美运行:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); //create a new one
layoutParams.weight = 1.0 f;
layoutParams.gravity = Gravity.CENTER; //this is layout_gravity
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setLayoutParams(layoutParams);

结束了 Kotlin 的这个扩展:

fun AlertDialog.withCenteredButtons() {
    val positive = getButton(AlertDialog.BUTTON_POSITIVE)
    val negative = getButton(AlertDialog.BUTTON_NEGATIVE)

    //Disable the material spacer view in case there is one
    val parent = positive.parent as? LinearLayout
    parent?.gravity = Gravity.CENTER_HORIZONTAL
    val leftSpacer = parent?.getChildAt(1)
    leftSpacer?.visibility = View.GONE

    //Force the default buttons to center
    val layoutParams = LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT
    )

    layoutParams.weight = 1f
    layoutParams.gravity = Gravity.CENTER

    positive.layoutParams = layoutParams
    negative.layoutParams = layoutParams
}

并用于:

.show().withCenteredButtons()
       IconButton(
            icon: Icon(
        Icons.logout,
        color: Colors.black,
        size: 40,
      ),
            onTap: () {
              return showDialog<String>(
                context: context,
                builder: (BuildContext context) => AlertDialog(
                  backgroundColor: Colors.white,
                  title: Text(
                    'Are you sure you want to logout',
                    textAlign: TextAlign.center,
                    style: TextStyle(color: Colors.black),
                  ),
                  actions: <Widget>[
                    Container(
                        child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        ///yes
                        TextButton(
                          onPressed: () =>
                              Navigator.pushNamedAndRemoveUntil(context,
                                  SecondScreen.id, (route) => false),
                          child: Container(
                            padding: EdgeInsets.all(10),
                            width: 70,
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(10),
                                color: mainColor),
                            child: Center(
                              child: Text(
                                'Yes',
                                style: TextStyle(color: Colors.white),
                              ),
                            ),
                          ),
                        ),

                        ///no
                        TextButton(
                          onPressed: () => Navigator.pop(context),
                          child: Container(
                            padding: EdgeInsets.all(10),
                            width: 70,
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(10),
                                color: Colors.white,
                                border: Border.all(color: mainColor)),
                            child: Center(
                              child: Text(
                                'No',
                                style: TextStyle(color: mainColor),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ))
                  ],
                ),
              );
          
            },
          ),