将AlertDialog按钮居中对齐。

38

我使用这些代码进行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)来使按钮水平居中,但是没有任何改变。按钮几乎在正确的位置。

10个回答

41

使用 crtn 的方法,但是不要改变 LayoutParam 的重心,而是将其宽度更改为 ViewGroup.LayoutParams.MATCH_PARENT;


2
运行良好。为了让像我这样的懒人更加清晰明了:请将 <positiveButtonLL.gravity = Gravity.CENTER;> 这一行替换为 <positiveButtonLL.width = ViewGroup.LayoutParams.MATCH_PARENT;>。 - Johan Lund
@JohanLund,如果我们有左、中、右三个按钮,我该如何设置? - VikaS GuttE

29

这对我有用:

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

参考 positiveBtn 是什么? - t0m
6
在 Android 7.1.1 上无效,但下面 @Scott Brown 的解决方案完全可行。 - mojomex

16
如果您希望同时拥有正面和负面按钮(大且居中),可以使用类似以下的内容:

Dialog Positive & Negative Buttons

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

11
这里是一些真正有效的内容。
这三个按钮(中性,积极和消极)的父控件是ButtonBarLayout,它继承自LinearLayout。要在LinearLayout中使视图居中,重量、宽度和layout_gravity(但不是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);

它可以工作。@frank的解决方案也可以工作。但我更喜欢这个。 - Stony
1
请注意,确保在 #getButton(AlertDialog.[BUTTON_YOU_WANT]) 中传递您想要显示的按钮,并确保在调用 alertDialog.show() 后设置 layoutParams。 - David Kim

9

我尝试了crtn和Scott Brown的方法,但都没有达到我想要的效果。

对于我使用的android.R.style.Theme_Material_Light_Dialog主题,crtn的解决方案并没有改变按钮的外观,而Scott Brown的解决方案使我的正按钮超出了对话框父级的边缘。

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

像crtn一样,我获取Button引用:

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

然后我将leftSpacer设置为View.GONE,将父类的gravity设置为CENTER_HORIZONTAL

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

这样做的好处是它不会破坏对话框按钮的层叠行为。 缺点是,如果内部布局更改,它将被打破,因此你的情况可能有所不同。


它有效了,这是我找到的最佳解决方案,非常感谢! - Simple
我和楼主遇到了同样的问题,将按钮重心改为中心对我没有用。你的解决方案非常好,谢谢。 - Morgane
这个解决方案最好,被接受的解决方案对我没有帮助,scottbrowns的解决方案对我也不适用,因为它只是将父元素匹配而已,而我想要将其左对齐而不是居中。 - Siddarth G

3

我最终选择了这个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()

2

我猜你正在使用支持库中的AlertDialog。

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


1
使用android.support.v7.app.AlertDialog可以将确认和取消按钮居中对齐。 android.app.AlertDialog会将按钮放置在顶部,底部留有16dp的空间。

0

您可以使用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();

in 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
       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),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ))
                  ],
                ),
              );
          
            },
          ),

2
你应该在回答中添加更多细节,而不仅仅是发布代码片段。 - Steven Mohr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接