将AlertDialog按钮对齐到中心

Bab*_*bad 24 android button alignment android-alertdialog

我使用此代码进行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;
}
Run Code Online (Sandbox Code Playgroud)

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

小智 33

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

  • 工作正常。对于像我这样的懒人来说超级清楚:将 <positiveButtonLL.gravity = Gravity.CENTER;> 替换为 <positiveButtonLL.width = ViewGroup.LayoutParams.MATCH_PARENT;> (2认同)

ckd*_*ckd 13

这对我有用:

    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);
Run Code Online (Sandbox Code Playgroud)

  • 在Android 7.1.1上不起作用,但是下面的@Scott Brown的解决方案可以正常工作。 (2认同)

Ali*_*dev 13

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

对话正面和负面按钮

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);
Run Code Online (Sandbox Code Playgroud)


fra*_*ank 7

尝试了crtn的方法和Scott Brown的修改,都没有呈现出我喜欢的样子。

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

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

Button像 crtn 一样抓取ref:

AlertDialog dialog = bld.create();
dialog.show(); //show() MUST be called before dialog.getButton
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
Run Code Online (Sandbox Code Playgroud)

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

LinearLayout parent = (LinearLayout) positiveButton.getParent();
parent.setGravity(Gravity.CENTER_HORIZONTAL);
View leftSpacer = parent.getChildAt(1);
leftSpacer.setVisibility(View.GONE);
Run Code Online (Sandbox Code Playgroud)

这样做的优点是不会破坏对话框的按钮堆叠行为。缺点是如果内部布局改变了就会断掉,所以YMMV。


小智 6

这确实有效。

ButtonBarLayout是3个按钮(中性,正ve和负)的父级,它扩展了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);
Run Code Online (Sandbox Code Playgroud)